Java I/O How to - Use Scanner to read a list of comma-separated values








Question

We would like to know how to use Scanner to read a list of comma-separated values.

Answer

//w  ww . j a v a2  s .  com
import java.util.*; 
import java.io.*; 
 
public class Main { 
  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); 
  } 
}

The code above generates the following result.