Scanner is a new Class added to the java 1.5 util package. This class can be used as an alternative to StringTokenizer but more powerful than StringTokenizer.I described it with examples to read input from Command line, from file with different delimiters,to search a text in the given line.
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; /** * This Class defines various uses of the Scanner Class in Java5 .We * can use scanner class to read data from file like StringTokenizer in previous * version of java.But Scanner is more powerful than StringTokenizer.We can * directly read inputs from command line ,file ,Inputstreamthis class has * depicts how to use scanner class to read input from command line,to read a * data from file with different delimiters,to search a word in a line. * */ public class UsageOfScanner { public static void main(String args[]) throws IOException { UsageOfScanner usc = new UsageOfScanner(); usc.avgWithCommaDelimiter(); System.out.println("--------------------------------"); usc.findInLine(); System.out.println("--------------------------------"); usc.readFromFile(); System.out.println("--------------------------------"); usc.avgFromCmd(); } /* * To read a value from the file with Single Space as delimiter * Using scanner there is no need to check and parse for primitive values. * We have api methods to get int,boolean, double... * */ void readFromFile() throws IOException{ int i; 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(); } /* * We can directly get the values from command line using * Scanner Class.This example get the input parameters from the * command line to find the average. * after entering the input parameters enter "done" to calculate average * */ void avgFromCmd(){ 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(); count++; } else { String str = conin.next(); if (str.equals("done")) break; else { System.out.println("Data format error."); return; } } } System.out.println("Average calculated From Cmd is " + sum / count); } /* * This method get the input parameters from a file with comma as delimiter and * finds the average of the values.We can use any string or even regex pattern as delimiter * */ void avgWithCommaDelimiter() 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 WithCommaDelimiter is " + sum / count); } /* * Scanner class can be used to find a specified string in a line. * Here we are searching for the occurance of Age and printing its value. * */ void findInLine(){ String instr = "Name: Joe Age: 28 ID: 77 "; Scanner conin = new Scanner(instr); conin.findInLine("Age:"); // find Age if (conin.hasNext()) System.out.println("Found Age: "+conin.next()); else System.out.println("Error!"); } }