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

class Apr11 {
	public static void main(String[] args) {

		// Create Scanner to read from "student.data"
		Scanner fin = null;

		try {
			fin = new Scanner(new File("student.data"));
		}
		catch(FileNotFoundException e) {
			System.out.println("File not found");
			System.exit(0);
		}

		// Change delimeters to match those used in students.data

		fin.useDelimiter(",|\n");

		// Read first line of file to get number of records

		int numRecords = fin.nextInt();

		// Create array to hold new Student references

		Student[] students = new Student[numRecords];
		
		// Create Student references and store in array

		for(int i = 0; i < numRecords; i++) {
			students[i] = new Student(
				fin.next(),
				fin.next(),
				fin.next(),
				fin.next(),
				fin.next(),
				fin.nextInt(),
				fin.nextInt()
			);
		}
		
		// print Student data to the screen
		for(int i = 0; i < numRecords; i++) {
			System.out.printf("[%d] %s\n", i, students[i]);
		}
		

		// Modify first name in one of the Student references
		Scanner kb = new Scanner(System.in);

		System.out.print("Enter record to change: ");
		int recordNumber = kb.nextInt();
		kb.nextLine();

		System.out.print("Enter new first name: ");
		String newFirstName = kb.nextLine();

		students[recordNumber].setFirstName(newFirstName);

		// print Student data to the screen
		for(int i = 0; i < numRecords; i++) {
			System.out.printf("[%d] %s\n", i, students[i]);
		}

		// Overwrite the data in student.data with data in array
		PrintWriter pw = null;

		try {
			pw = new PrintWriter("student.data");
		}
		catch(FileNotFoundException e) {
			System.out.println("pw: file not found");
			System.exit(0);
		}

		pw.printf("%d\n", numRecords);

		for(int i = 0; i < numRecords; i++) {
			pw.printf("%s\n", students[i]);
		}
		
		pw.close();	

	}
}
