scg/ch12/FileTwoNumbersRight

From FANG

Jump to: navigation, search

01 package scg.ch12;
02 
03 import java.io.File;
04 import java.io.FileNotFoundException;
05 import java.io.PrintWriter;
06 import java.util.Scanner;
07 
08 /**
09  * Prompt user for two integers. Print the sum of the two integers.
10  * Program then halts.
11  */
12 public class FileTwoNumbersRight {
13   /**
14    * The main program. Prompt user for two integers and add them
15    * together.
16    *
17    @param  args  command-line arguments; ignored by this program
18    */
19   public static void main(String[] args{
20     System.out.println("FileTwoNumbersRight:");
21     Scanner keyboard = new Scanner(System.in);
22 
23     System.out.print("Results file name: ");
24     String fname = keyboard.nextLine();
25     File outFile = new File(fname);
26     try {
27       PrintWriter out = new PrintWriter(outFile);
28 
29       System.out.print("Number: ");
30       int first = keyboard.nextInt();
31 
32       System.out.print("Number: ");
33       int second = keyboard.nextInt();
34 
35       // Print results in the file
36       out.println("Sum of " + first + " + " + second + " = " +
37         (first + second));
38       out.close();
39     } catch (FileNotFoundException e{
40       e.printStackTrace();
41       System.err.println(
42         "Program cannot make useful progress: terminating.");
43       System.exit(1);
44     }
45   }
46 }
47 
48 //Uploaded on Mon Mar 29 21:38:38 EDT 2010


Download/View scg/ch12/FileTwoNumbersRight.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games