File I/O Part 1 #121 (Records are always broken...)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name: HighScore
/// File Name:HighScore.java
/// Date Finished: 5/18/2016

    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class HighScore 
    {
        public static void main(String[] args) 
        {
            PrintWriter fileOut;
            Scanner keyboard = new Scanner( System.in );
    
            try
            {
                fileOut = new PrintWriter("score.txt");
            }
            
            catch(IOException e)
            {
                System.out.println( "Sorry, there has been an error trying to open 'score.txt'" );
                fileOut = null;
                System.exit(1);
            }
            
            System.out.println( "You got a high score!!" );
            System.out.println();
            
            System.out.print( "Please enter your score: " );
            int score = keyboard.nextInt();
            
            System.out.print( "Please enter your name: " );
            String name = keyboard.next();
            
            System.out.println();
            System.out.println( "Your data has been stored in 'score.txt'" );
            
            fileOut.println( "Name: " + name );
            fileOut.println( "Score: " + score );
    
            fileOut.close();
        }
    } 





    

Picture(s) of the output

Assignment 7 Assignment 7

Back to Homepage