Program #125

Code

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

      import java.util.Scanner;
        import java.io.File;
        
        public class Numbers {
            
            public static void main(String[] args) throws Exception{
                
                Scanner kb = new Scanner(System.in);
                Scanner fileOut1 = new Scanner(new File("3nums1.txt"));
                Scanner fileOut2 = new Scanner(new File("3nums2.txt"));
                Scanner fileOut3 = new Scanner(new File("3nums3.txt"));
                
                int choice, num1, num2, num3, sum;
                
                System.out.print("Which file would you like to draw from? (1-3) ");
                choice = kb.nextInt();
                
                if (choice == 1)
                {
                    num1 = fileOut1.nextInt();
                    num2 = fileOut1.nextInt();
                    num3 = fileOut1.nextInt();
                    
                    sum = num1 + num2 + num3;
                }
                else if (choice ==2)
                {
                    num1 = fileOut2.nextInt();
                    num2 = fileOut2.nextInt();
                    num3 = fileOut2.nextInt();
                
                    sum = num1 + num2 + num3;
                }            
                else
                {
                    num1 = fileOut3.nextInt();
                    num2 = fileOut3.nextInt();
                    num3 = fileOut3.nextInt();
                
                    sum = num1 + num2 + num3;
                }       
                
                System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
            }
        } 
             
    

Picture of the output

Assignment 125