Asignment: #121 Writing Files with High Scores
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: HighScore
/// File Name: HighScore.html
/// Date Finished: 5/21/2016
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.InputMismatchException;
public class HighScore {
public static void main(String[] args) {
PrintWriter fileOut;
int score = 0;
String Name;
Scanner keyboard = new Scanner(System.in);
do
{
try
{
System.out.println();
System.out.print( "You got a High Score!!!\n\n Please enter your score: " );
score = keyboard.nextInt();
}
catch (InputMismatchException e)
{
System.out.println();
System.out.println( "Has to be integer and positive!!!" );
}
} while (score <= 0);
System.out.println();
System.out.print( "Please enter your name: " );
Name = keyboard.next();
try
{
fileOut = new PrintWriter("score.txt");
}
catch(IOException e)
{
System.out.println("Sorry, I can't open the file 'receipt.txt' for editing.");
System.out.println("Maybe the file exists and is read-only?");
fileOut = null;
System.exit(1);
}
fileOut.println( Name+": "+score );
fileOut.close();
System.out.println( "Score saved in score.txt" );
}
}
Picture of the output