Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

In this page you can find the example usage for java.util Scanner Scanner.

Prototype

public Scanner(ReadableByteChannel source) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";

    Scanner scanner = new Scanner(s);

    // skip the word that matches the pattern .com
    scanner.skip(Pattern.compile(".com"));

    System.out.println(scanner.nextLine());

    scanner.close();//  w w  w .j  a v a2s  . c om
}

From source file:Calculator.java

public static void main(String[] args) {
    System.out.println("type something like: 1+3");
    Scanner scanner = new Scanner(System.in);
    double n1 = Double.NaN;
    double n2 = Double.NaN;
    String operation = null;//w  w  w. j a va2  s .c  om

    try {
        n1 = scanner.nextDouble();
        operation = scanner.next();
        n2 = scanner.nextDouble();
        double result = calculate(n1, n2, operation);
        System.out.printf("%s %s  %s  = %.2f%n", n1, operation, n2, result);
    }

    catch (Exception e) {
        System.out.println("An invalid expression.");
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a string of world, with horizon of 10
    System.out.println(scanner.findWithinHorizon("com", 10));

    // find a string of world, with horizon of 20
    System.out.println(scanner.findWithinHorizon("=", 20));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();//from w ww. j  a  v a  2 s  .  c  o m
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a pattern of 2 letters before com, with horizon of 5
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 5));

    // find a pattern of 2 letters before com, with horizon of 10
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 10));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();//from w w  w.ja  va 2s. c o m
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // check if the scanner has a token
    System.out.println(scanner.hasNext());

    // print the rest of the string
    System.out.println(scanner.nextLine());

    // check if the scanner has a token after printing the line
    System.out.println(scanner.hasNext());

    scanner.close();//from w  ww. j a  v  a2  s  .  com
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";

    Scanner scanner = new Scanner(s);

    System.out.println(scanner.nextLine());

    // change the delimiter of this scanner
    scanner.useDelimiter(Pattern.compile(".ll."));

    // display the new delimiter
    System.out.println(scanner.delimiter());

    scanner.close();/*from w  ww . ja v a 2s  .  c om*/
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true";

    Scanner scanner = new Scanner(s);

    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {

        // if the next is a double, print found and the double
        if (scanner.hasNextDouble()) {
            System.out.println("Found :" + scanner.nextDouble());
        }/*  ww  w .  j  a va 2 s. co m*/

        // if a double is not found, print "Not Found" and the token
        System.out.println("Not Found :" + scanner.next());
    }

    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";

    Scanner scanner = new Scanner(s);

    System.out.println(scanner.nextLine());

    // change the locale of this scanner
    scanner.useLocale(Locale.US);

    // change the radix of this scanner
    scanner.useRadix(30);//from   w  w w  .java2  s.  com

    // reset and check the values for radix and locale, which are the default
    scanner.reset();
    System.out.println(scanner.radix());
    System.out.println(scanner.locale());

    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {
    String content = " <2008-10-07> hi <test>" + "   <2008-11-26>  user <test>" + "   <2008-11-28><aaaa>  ";
    Scanner sc = new Scanner(content).useDelimiter("\\s*[<>]\\s*");
    while (sc.hasNext()) {
        System.out.printf("[%s|%s|%s]%n", sc.next(), sc.next(), sc.next());
        if (sc.hasNext())
            sc.next();/*  w  w  w  .  ja  v a2  s. c o m*/
    }
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException {

    Scanner scanner = new Scanner(new FileInputStream("c:/text.txt").getChannel());

    System.out.println(scanner.nextLine());

    // change the radix of the scanner
    scanner.useRadix(32);//from   w w w.  j  av  a2  s.c om

    // display the new radix
    System.out.println(scanner.radix());

    scanner.close();
}