Program #128

Code

   
    /// Name: Afaf Nabeeha
    /// Period: 7
    /// Program Name: Summing Several Numbers From any File
    /// File Name: Several.java
    /// Date Finished: 3/8/2016 public class Several

        import java.util.Scanner;
        import java.io.File;
        
        public class Several {
            
            public static void main(String[] args) throws Exception {
                
                Scanner kb = new Scanner(System.in);
                
                System.out.println("Which file would you like to add from?");
                System.out.print("> ");
                String file = kb.next();
                System.out.println("");
                
                while (file.equals("5nums.txt") || file.equals("6nums.txt") || file.equals("7nums.txt") || file.equals("8nums.txt")) {
                    
                    Scanner reader = new Scanner(new File(file));
                    int total = 0;
                    
                    while (reader.hasNext()) {
                        
                        int a = reader.nextInt();
                        
                        System.out.print(a + " ");
                        
                        total = total + a;
                    }
                    
                    System.out.println("Total: " + total);
                    System.out.println("");
                    System.out.println("Which file would you like to add from?");
                    System.out.print("> ");
                    file = kb.next();
                    System.out.println("");
                }
            }
        }       
             
    

Picture of the output

Assignment 128