Scanner: nextDouble() : Scanner : java.util : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP
Java by API Home »  java.util   » [  Scanner  ]   
 



Scanner: nextDouble()


/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12

 */
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) throws IOException {
    int i;
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("string true false 1 2 3 4.12");
    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();
  }
}
           
       
Related examples in the same category
1.  new Scanner(FileReader file)
2.  new Scanner(InputStream source)
3.  new Scanner(String instr)
4.  new Scanner(Readable source)
5.  Scanner: findInLine(String str)
6.  Scanner: hasNext()
7.  Scanner: hasNextBoolean()
8.  Scanner: hasNextDouble()
9.  Scanner: hasNextInt()
10.  Scanner: next()
11.  Scanner: nextBoolean()
12.  Scanner: nextInt()
13.  Scanner: useDelimiter(String pattern)
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.