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[]) {
    String instr = "Name: Joe Age: 28 ID: 77";

    Scanner conin = new Scanner(instr);

    conin.findInLine("Age:"); // find Age

    if (conin.hasNext())
        System.out.println(conin.next());
    else/* w  w w .  j av a  2s.  c  o  m*/
        System.out.println("Error!");

}

From source file:MainClass.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:", 0);

    if (sc.hasNext())
        System.out.println(sc.next());
    else/*from  www  .ja v a2  s. c  o  m*/
        System.out.println("Error!");
}

From source file:MainClass.java

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

    int i;/*  ww w  . j a  va 2  s .co m*/
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";
    Float f = 1.2385f;/*from w ww . j a va 2s  .c o  m*/
    s = s + f;

    Scanner scanner = new Scanner(s);

    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        scanner.next();
        if (scanner.hasNextFloat()) {
            System.out.println("Found :" + scanner.nextFloat());
        }
    }

    scanner.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("IDs:", 0);

    if (sc.hasNext())
        System.out.println(sc.next());
    else//from   www  . j a v a 2 s . c o m
        System.out.println("Error!");
}

From source file:Main.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:", 100);

    if (sc.hasNext())
        System.out.println(sc.next());
    else/*from   ww w.  ja  va2  s .  co  m*/
        System.out.println("Error!");
}

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());
        }//w w  w  .j  a v a 2s. com

        // 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) throws Exception {
    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter(",");
        while (lineScanner.hasNext()) {
            String part = lineScanner.next();
            System.out.print(part + ", ");
        }//from w w w .  j a  v a 2 s.  c  o m
        System.out.println();
    }
}

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();//ww w. java2  s.co  m
}

From source file:Main.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();//from  w w  w.  j  ava 2  s.co 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);
}