/* Written by Jen Chen Updated on March the 4th, 2003 Copyrighted by Jen Chen */ import java.io.*; import java.util.*; import java.text.*; public class numberFormat { public static void main(String[] args) throws IOException{ String file = null, line; file = "score.txt"; BufferedReader buf = null; int lineNum = 0; buf = new BufferedReader(new FileReader(file)); StringTokenizer stToken; //format the values up to 2 decimal accuracy. DecimalFormat df = new DecimalFormat("####.00"); NumberFormat nf = NumberFormat.getPercentInstance(); System.out.println(nf.format(0.47)); //display 0.47 as 47%. while((line = buf.readLine()) != null){ stToken = new StringTokenizer(line, ","); double sum = 0.0; while(stToken.hasMoreTokens()){ if(stToken.countTokens() % 4 == 0){ //if this is the 1st token. String name = stToken.nextToken().trim(); System.out.println("Name is : " + name); }else{ int val = Integer.parseInt(stToken.nextToken().trim()); sum += val; System.out.println(" Score: " + val); }//end of IF-statement }//end of WHILE(Tokenizer) System.out.println("\tTotal score: " + sum); System.out.println("\tAverage: " + df.format(sum/3)); } //END of outer WHILE. } //END of main() } //END of class FILE_IO2