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);

    while (scanner.hasNext()) {

        // if the next is a int, print found and the int with radix 4
        if (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt(4));
        }// www.  j a  v a 2s.  c  o m
        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);

    while (scanner.hasNext()) {

        // if the next is BigInteger, print "Found" and the Integer
        if (scanner.hasNextBigInteger()) {
            System.out.println("Found :" + scanner.nextBigInteger());
        }//w ww .ja  va 2 s.co  m

        // if a BigInteger 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 \n 1 + 1 = 2.0 ";

    Scanner scanner = new Scanner(s);

    // print the next line
    System.out.println(scanner.nextLine());

    // check if there is a next line again
    System.out.println(scanner.hasNextLine());

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

    // check if there is a next line again
    System.out.println(scanner.hasNextLine());

    scanner.close();/*from w  w  w  .  j a va  2  s .  co  m*/
}

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);

    while (scanner.hasNext()) {

        // if the next is byte with radix 7, print found and the byte
        if (scanner.hasNextByte()) {
            System.out.println("Found :" + scanner.nextByte(7));
        }/*from w w  w. j av  a 2 s.  c  o m*/

        // if a Byte 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);

    while (scanner.hasNext()) {

        // if the next is BigDecimal, print found and the decimal
        if (scanner.hasNextBigDecimal()) {
            System.out.println("Found :" + scanner.nextBigDecimal());
        }//from   ww w  .  j a  va2 s.  c  o  m

        // if a BigDecimal 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 = "Hello true World! 1 + 1 = 2.0 ";

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {

        // if the next is boolean, print found and the boolean
        if (scanner.hasNextBoolean()) {
            System.out.println("Found :" + scanner.nextBoolean());
        }//  ww  w . java 2  s .  c  om

        // if a boolean 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) throws Exception {
    File file = new File("data.txt");

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        System.out.println(line);
    }/*from   www  .j  a v  a 2  s . c  o m*/
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 3.12345f";

    Scanner scanner = new Scanner(s);

    // assign locale as US to recognize float numbers in a string
    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // check if the scanner's next token is a float
        System.out.println(scanner.hasNextFloat());
        System.out.println(scanner.next());
    }// ww  w  . j a v  a  2  s .co m
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {
        System.out.println("Not Found :" + scanner.next());
        if (scanner.hasNextLong()) {
            System.out.println("Found :" + scanner.nextLong(20));
        }// w w  w. jav  a2s . c  om
    }
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {
    String s = "java2s.com 1 + 1 = 2.0 ";

    Scanner scanner = new Scanner(s);

    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // check if the scanner's next token is a short with a radix 4
        System.out.println(scanner.hasNextShort(4));

        System.out.println(scanner.next());
    }/*from w  w  w  .  j av a2s . co m*/

    scanner.close();
}