Assignemnt #128 Summing More Numbers

Code

    /// Name: JJ Deng
    /// Period: 6
    /// Program Name:Summing Several Numbers
    /// File Name: MoreNumMore.java
    /// Date Finished: 6/2/2016
  
        import java.io.File;
        import java.util.Scanner;
        import java.util.InputMismatchException;
        
    public class MoreNumMore {
        
            public static void main(String[] args) throws Exception {
        
                int sum = 0;
                String fileName = "";
                Scanner keyboard = new Scanner(System.in);
        
                while (!fileName.equals("exit"))
                {
                    System.out.print("Which file would you like to read from?\n>");
                    fileName = keyboard.next();
        
                    if (!fileName.equals("exit"))
                    {
                        System.out.print("Reading numbers from file \"" + fileName + "\":\n\n>");
        
                        Scanner fileIn = new Scanner(new File(fileName));
        
                        while(fileIn.hasNext())
                        {
                            try {
                                int temp = fileIn.nextInt();
                                System.out.print(" " + temp);
                                sum += temp;
                            }
                            catch (InputMismatchException e){};
                        }
        
                        fileIn.close();
        
                        System.out.println("\nSum is " + sum);
                    }
                }
            }
        }

    

Picture of the output

Assignment 120