/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // AreaScanner: examples of use of Scanner and printf (JDK 1.5) import java.io.*; // Standard I/O classes import java.util.*; // scanner class public class Area_Scanner { public static final double PI = 3.141593; // Constants public static void main(String[] argv) throws IOException, FileNotFoundException { double r, A; // Connect Scanner to std in Scanner sc1 = new Scanner(System.in); System.out.println("Key in your name & r on 1 or more lines"); // Read String, read double String name = sc1.next(); r = sc1.nextDouble(); System.out.printf("Hi "+name); System.out.printf("\n radius = "+r); System.out.printf("\n\n Enter new name and r in Name.dat\n"); // Input file Scanner sc2 = new Scanner(new File("Name.dat")); // Open file System.out.printf("Hi %s\n",sc2.next()); // Read, print ln 1 r = sc2.nextDouble(); // Read ln 2 System.out.printf("r = %5.1f\n",r); // Print ln 2 A = PI * r * r; System.out.printf("Done, look in A.dat\n"); // Print to screen // Open output file PrintWriter q = new PrintWriter (new FileOutputStream("A.dat"), true); q.printf("r = %5.1f\n" ,r); // File output q.printf("A = %8.3f\n",A); System.out.printf("r = %5.1f\n" , r); // Screen output System.out.printf("A = %8.3f\n" , A); // Integer input System.out.printf("\n\n Now key in your age as an integer\n"); int age = sc1.nextInt(); // Read int System.out.printf(age+"years old, you don't look it!" ); sc1.close(); sc2.close(); // Close inputs } }