import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;

class Exam3 {

	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		while(true) {
			printMenu();
			int choice = kb.nextInt();
			switch(choice) {
				case 1:
					saveQuizScores();
					break;
				case 2:
					printQuizScores();
					break;
				case 3:
					return;
			}
		}

	}

	static void printMenu() {
		System.out.println("1) Save quiz scores");
		System.out.println("2) Print quiz scores");
		System.out.println("3) Exit");
		System.out.print("Choose a menu option: ");
	}

	static void saveQuizScores() {
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new File("scores.txt"));
		}
		catch(FileNotFoundException e) {
			System.out.println("File not found for writing");
			return;
		}

		Scanner kb = new Scanner(System.in);
		while(true) {
			System.out.print("Enter name (or stop): ");
			String name = kb.next();

			if(name.equals("stop")) {
				pw.close();
				return;
			}

			System.out.print("Enter score: ");
			int score = kb.nextInt();

			pw.printf("%s:%d\n", name, score);
		}
	}

	static void printQuizScores() {
		Scanner fin = null;
		try {
			fin = new Scanner(new File("scores.txt"));
			fin.useDelimiter(":|\n");
		}
		catch(FileNotFoundException e) {
			System.out.println("File not found for reading");
			return;
		}

		while(fin.hasNext()) {
			String name = fin.next();
			int score = fin.nextInt();
			System.out.printf("%s %d\n", name, score);
		}
	}


}
