new Scanner(Readable source) : Scanner « java.util « Java by API






new Scanner(Readable source)

 
/*
 */
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 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(); 
 
    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); 
  }
}
           
         
  








Related examples in the same category

1.new Scanner(FileReader file)
2.new Scanner(InputStream source)
3.new Scanner(String instr)
4.Scanner: findInLine(String str)
5.Scanner: findWithinHorizon(String pattern, int horizon)
6.Scanner: hasNext()
7.Scanner: hasNextBoolean()
8.Scanner: hasNextDouble()
9.Scanner: hasNextInt()
10.Scanner: hasNextLine()
11.Scanner: next()
12.Scanner: nextBoolean()
13.Scanner: nextDouble()
14.Scanner: nextInt()
15.Scanner: nextLine()
16.Scanner: useDelimiter(String pattern)