Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if this scanner has another token in its input.

Usage

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();/*ww  w.j av  a 2  s.  c o m*/

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(new File("fileName"));
    scanner.useDelimiter(System.getProperty("line.separator"));
    while (scanner.hasNext()) {
        parseLine(scanner.next());//from  w  w w .ja v a2s.com
    }
    scanner.close();
}

From source file:MainClass.java

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

    int count = 0;

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

    while (conin.hasNext()) {
        if (conin.hasNextDouble()) {
            System.out.println(conin.nextDouble());
            count++;/*  w w  w.  ja v  a  2s. com*/
        }
        if (count == 3) {
            System.exit(0);

        }
    }

}

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 . java  2s . com
    scanner.close();
}

From source file:ScanXan.java

public static void main(String[] args) throws IOException {
    Scanner s = null;
    try {//ww w  .j a va2 s  . c  o m
        s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

        while (s.hasNext()) {
            System.out.println(s.next());
        }
    } finally {
        if (s != null) {
            s.close();
        }
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

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

    while (scanner.hasNext()) {
        // check if the scanner's next token is a double
        System.out.println(scanner.hasNextDouble());
        System.out.println(scanner.next());
    }//from  w  w  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 ";
    Long l = 1234567890987654L;//  w  ww  .java 2 s.  co m
    s = s + l;

    Scanner scanner = new Scanner(s);

    // use US locale in order to be able to identify long in a string
    scanner.useLocale(Locale.US);

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

        System.out.println(scanner.next());
    }
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com ";
    Long l = 1234567890987654L;/*from   ww w  .ja v a2  s  .  c  o m*/
    s = s + l;

    Scanner scanner = new Scanner(s);

    // use US locale in order to be able to identify long in a string
    scanner.useLocale(Locale.US);

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

        System.out.println(scanner.next());
    }
    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);

    // use US locale in order to be able to identify shorts in a string
    scanner.useLocale(Locale.US);

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

        System.out.println(scanner.next());
    }/*ww w. j  a  v a2s.  c  o m*/

    scanner.close();
}

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 a  2  s.  com
            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);
}