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

    Scanner scanner = new Scanner(s);

    // check if next token is "java"
    System.out.println(scanner.hasNext("java"));

    // find the last match and print it
    System.out.println(scanner.match());

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

    scanner.close();/*from  w  w  w  .j av  a2 s  . c  om*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Scanner conin = new Scanner(System.in);

    int count = 0;
    double sum = 0.0;

    System.out.println("Enter numbers to average.");

    while (conin.hasNext()) {
        if (conin.hasNextDouble()) {
            sum += conin.nextDouble();/*  w  w  w  .j a v a2  s.  c om*/
            count++;
        } else {
            String str = conin.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("Data format error.");
                return;
            }
        }
    }

    System.out.println("Average is " + sum / count);
}

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 (scanner.hasNextShort()) {
            System.out.println("Found :" + scanner.nextShort());
        }//from w  w  w.j a  va 2  s. co 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 false 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {
        // check if the scanner's next token is a boolean
        System.out.println(scanner.hasNextBoolean());

        System.out.println(scanner.next());
    }/*from w w w .j a va  2  s.  c  o  m*/
    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 radix of the scanner
    scanner.useRadix(32);//from ww  w  .  jav  a  2  s. c  o  m

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

    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 (scanner.hasNextShort()) {
            System.out.println("Found :" + scanner.nextShort(4));
        }//from w ww . j  a v a 2s  .c  om
        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 true ";

    Scanner scanner = new Scanner(s);

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

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

    scanner.close();/*from   w  w w.  j  a  v  a2 s  . c o  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 (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt());
        }//from  ww  w.j a  v a 2 s.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);

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

    // change the delimiter of this scanner
    scanner.useDelimiter("Wor");

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

    scanner.close();/*from w  ww  . j  av a2  s. 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);

    while (scanner.hasNext()) {

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

    scanner.close();
}