File I/O Part 1 #125 (Its a doosy)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name: Summing Three Numbers from any Files
/// File Name:STN.java
/// Date Finished: 5/27/2016

    
    import java.io.File;
    import java.util.Scanner;
    
    public class STNAF
    {
        public static void main (String[] args) throws Exception
        {
            Scanner keyboard = new Scanner(System.in);
            int a, b, c, sum;
            String again;
            
            System.out.println();
            
            do
            {
                System.out.print( "Which file would you like to read numbers from: " );
                String answer = keyboard.next();
    
                if ( answer.equals("3nums1.txt"))
                {
                    Scanner fileIn = new Scanner(new File("3nums1.txt"));
    
                    System.out.println( "Retrieving information from \"3nums1.txt\" ... " );
    
                    a = fileIn.nextInt();
                    b = fileIn.nextInt();
                    c = fileIn.nextInt();
    
                    fileIn.close();
    
                    sum = a + b + c;
                    System.out.println( a + " + " + b + " + + " + c + " = " + sum );
                }
    
                if ( answer.equals("3nums2.txt"))
                {
                    Scanner fileIn = new Scanner(new File("3nums2.txt"));
    
                    System.out.println( "Retrieving information from \"3nums2.txt\" ... " );
    
                    a = fileIn.nextInt();
                    b = fileIn.nextInt();
                    c = fileIn.nextInt();
    
                    fileIn.close();
    
                    sum = a + b + c;
                    System.out.println( a + " + " + b + " + + " + c + " = " + sum );
                }
    
                if ( answer.equals("3nums3.txt"))
                {
                    Scanner fileIn = new Scanner(new File("3nums3.txt"));
    
                    System.out.println( "Retrieving information from \"3nums3.txt\" ... " );
    
                    a = fileIn.nextInt();
                    b = fileIn.nextInt();
                    c = fileIn.nextInt();
    
                    fileIn.close();
    
                    sum = a + b + c;
                    System.out.println( a + " + " + b + " + + " + c + " = " + sum );
                }
    
                System.out.println();
    
                System.out.print( "Would you like to read numbers from another file? " );
                again = keyboard.next();
                
            } while ( again.equals("yes"));
            
            if ( again.equals("no"))
                System.out.println( "goodbye" );
            
        }
    }  





    

Picture(s) of the output

Assignment 7

Back to Homepage