Example usage for java.util Scanner hasNextDouble

List of usage examples for java.util Scanner hasNextDouble

Introduction

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

Prototype

public boolean hasNextDouble() 

Source Link

Document

Returns true if the next token in this scanner's input can be interpreted as a double value using the #nextDouble method.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String instr = "10 99.88 scanning is easy.";
    Scanner conin = new Scanner(instr);

    while (conin.hasNext()) {
        if (conin.hasNextDouble()) {
            System.out.println(conin.nextDouble());
        }/*from w  ww  .  java  2  s  .  c o m*/
    }

}

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();/*w  w w.  ja  v a2 s .  c  o m*/

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

    Scanner src = new Scanner(fin);

    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:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();/*from ww w  . j  av  a2  s . c  o m*/

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

    Scanner src = new Scanner(fin);

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

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 . ja va2  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: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();//from ww w  . j a v 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: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++;//from w  w  w  .  j  a va2s.com
        }
        if (count == 3) {
            System.exit(0);

        }
    }

}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();//from   w w w .  j a  v  a  2s  .  c  o m

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            System.out.println("int: " + src.nextInt());
        } else if (src.hasNextDouble()) {
            System.out.println("double: " + src.nextDouble());
        } else if (src.hasNextBoolean()) {
            System.out.println("boolean: " + src.nextBoolean());
        } else {
            System.out.println(src.next());
        }
    }
    fin.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);

    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 .ja  va2s.c  om*/

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

    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();//from  ww w .jav  a  2s. 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:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;/*  ww w . jav a2s. 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();
}