Scanner and printf()

Last Modified On : 21-Aug-2004


// Demo of Scanner and Printf 

public class ScannerAndPrintf
{ 
   public static void main(String args[]) throws Exception
   {
     Scanner sc= new Scanner(System.in);
     int i = sc.nextInt(); // Read an integer
     System.out.printf("Value entered is = %d\n",i);

     // take numbers from a file. One number in one line 
     sc = new Scanner(new File("numbers.txt"));
     int a;
     while (sc.hasNextInt())
     {
        a = sc.nextInt();
        System.out.println("Value = " + a);
     }

     //  commanumbers.txt has numbers separated by ,
     sc = new Scanner(new File("commanumbers.txt"));
     Pattern p = Pattern.compile(",");
     sc.useDelimiter(p);
     while (sc.hasNextInt())
     {
        a = sc.nextInt();
        System.out.println("Value = " + a);
     }
  } // end of main     
}