I am trying to help a buddy of mine with this program and I can't seem to figure it out. I am still working on it but any help would be appreciated. The program is suppose to write to and read from a file: student names, scores, make a report, edit and delete data from the file. Right now it compiles but it gives an error at runtime right at the fileOut.write(firstName[test]); line. Again thank you for any help.
import java.util.*; import java.io.*; public class finalProjectGradingProgram { public static void main(String[] args) throws IOException { Scanner myScanner = new Scanner(System.in); File file = new File("studentFile.txt"); BufferedWriter fileOut = new BufferedWriter(new FileWriter(file)); BufferedReader fileIn= new BufferedReader(new FileReader("studentFile.txt")); BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); int [][] score = new int[27][50]; int [] numberOfAssignments = new int[27]; String [] firstName = new String[27]; String [] lastName = new String[27]; int [] studentId = new int[27]; String end, assignmentTest, searchNumber, scoreTemp, studentIdTemp, empty; String restartTemp, numberOfStudentsString, assignmentIndexString, editEntryString; int numberOfStudentsTest; int i, h, l, p, y, s, entry; int firstNameCounter,arrayCounter, arrayCounter2, scoreCounter, scoreCounter2; int assignmentTestInt, test; int searchNumberInt, searchLoop, index; int editLoop, editEntry; int assignmentIndex; int parseInt = 0; int scoreTempInt; int restart, restartTest; int average, averageTemp; int numberOfStudents; int emptyFinder; empty= "empty"; end= "end"; //sets all the entrys in array score = -1 for later error checking. for(arrayCounter = 0; arrayCounter < 26; arrayCounter++) { for(scoreCounter= 0; scoreCounter < 50; scoreCounter++) { score[arrayCounter][scoreCounter] = -1; } } for(y=0;y<27;y++) { firstName[y]= "empty"; } numberOfStudentsTest= 0; for(arrayCounter2 = 0; arrayCounter2 < 26; arrayCounter2++) { studentId[arrayCounter2]= fileIn.read(); firstName[arrayCounter2]= fileIn.readLine(); lastName[arrayCounter2]= fileIn.readLine(); numberOfAssignments[arrayCounter2]= fileIn.read(); for(scoreCounter2= 0; scoreCounter2 < 50; scoreCounter2++) { score[arrayCounter2][scoreCounter2] = fileIn.read(); } } numberOfStudentsTest=fileIn.read(); restart=4; entry = 0; while(entry > 0 && entry < 5) { System.out.println("If you wish to add a student please press 1"); System.out.println("If you wish to modify a student please press 2"); System.out.println("If you wish to delete a student please press 3"); System.out.println("If you wish to run a report on a student please press 4"); System.out.println("If you wish to end the program please press 5"); //entry = dataIn.readLine(); //parseInt = Integer.parseInt(entry); entry = myScanner.nextInt(); numberOfStudentsTest = 0; //start of the menu system. I still need to work on the modify and delete portions. //the if statement works in conjunction with the menu if(parseInt == 1) { System.out.println("Please enter the amount of students you wish to add"); numberOfStudentsString = dataIn.readLine(); numberOfStudents = Integer.parseInt(numberOfStudentsString); //This asks the user to enter the number of students they wish to add allowing them to effectivly end the loop with out the need to break it. for(i=(0); i < (numberOfStudents); i++) //The for loop is to run though it 26 times. This needs to be edited to ask for or find where the teacher left off last time. It will also systematiclly fill in the arrays. { System.out.println("Please enter student's last name, Enter end to quit:"); lastName[i]=(String) dataIn.readLine(); if (lastName[i].equals(end)) { break; } System.out.println("Please enter student first name, Enter end to quit:"); firstName[i]=(String) dataIn.readLine(); if (firstName[i].equals(end)) { break; } System.out.println("Please enter the student ID number, Enter -5 to quit:"); studentIdTemp = dataIn.readLine(); studentId[i]= Integer.parseInt(studentIdTemp); if(studentId[i] == -5) { break; } for (h = 0; h<25;h++) { System.out.println("Please enter student "+ (h+1) +" assignment score, Enter -5 to quit:"); assignmentTest = dataIn.readLine(); assignmentTestInt = Integer.parseInt(assignmentTest); if(assignmentTestInt == -5) { break; } else { score[i][h] = assignmentTestInt; numberOfAssignments[i] = h+1; } } numberOfStudentsTest= (numberOfStudentsTest+1); } }//if if(parseInt == 2) { System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) { if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } } index = searchLoop; System.out.println("Found Student"); System.out.println("Current Student Information"); System.out.println(studentId[index]); System.out.println(firstName[index]); System.out.println(lastName[index]); System.out.println(numberOfAssignments[index]); for(editLoop=0;editLoop < numberOfAssignments[index+1]; editLoop++) { System.out.print(score[index][editLoop]); } System.out.println("What would you like to do?"); System.out.println("Enter 1 to edit student ID"); System.out.println("Enter 2 to edit First Name"); System.out.println("Enter 3 to edit Last Name"); System.out.println("Enter 4 to edit Scores"); editEntryString = dataIn.readLine(); editEntry = Integer.parseInt(editEntryString); if(editEntry == 1) { System.out.println("Please enter the new student ID"); studentIdTemp= dataIn.readLine(); studentId[index]= Integer.parseInt(studentIdTemp); } if(editEntry == 2) { System.out.println("Please enter the new first name of the student"); firstName[index] = dataIn.readLine(); } if(editEntry == 3 ) { System.out.println("Please enter the new last name of the student"); lastName[index] = dataIn.readLine(); } if(editEntry == 4) { System.out.println("Please enter the number of the assignment you would like to edit"); assignmentIndexString = dataIn.readLine(); assignmentIndex = Integer.parseInt(assignmentIndexString); assignmentIndex = (assignmentIndex - 1); System.out.println("Please enter the new score for the assignment"); score[index][assignmentIndex]=dataIn.read(); } } if(parseInt == 3) { System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) { if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } } index = searchLoop; firstName[index] = " "; lastName[index] = " "; studentId[index] = -1; numberOfAssignments[index] = -1; for(p=0;p<25;p++) { score[index][p]= -1; } } if(parseInt == 4) { System.out.println("Who do you wish to run the report for?"); System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) { if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } } index = searchLoop; System.out.println(firstName[index]); System.out.println(lastName[index]); System.out.println(studentId[index]); System.out.println(numberOfAssignments[index]); for(int q=0;q<numberOfAssignments[index];q++) { System.out.print(score[index][q] + " "); } averageTemp = 0; for(int j=0;j < numberOfAssignments[index];j++) { averageTemp= averageTemp + score[index][j]; } average=0; //Seems to just skip over this section entirely. I have no idea why. average = averageTemp/numberOfAssignments[index]; System.out.println(average); } if(parseInt == 5) { System.exit(0); } System.out.println("Would you like to restart the program? If you wish to restart enter the number 4"); restartTemp= dataIn.readLine(); restartTest = Integer.parseInt(restartTemp); restart= restartTest; }//while for menu for(test=0;test<50;test++) { fileOut.write(studentId[test]); fileOut.write(firstName[test]); fileOut.write(lastName[test]); fileOut.write(numberOfAssignments[test]); for(l=0;l<50;l++) { fileOut.write(score[test][l]); } } fileOut.write(numberOfStudentsTest); fileOut.close(); } }
Please visit the following link:
http://www.roseindia.net/tutorial/java/swing/studentApplication.html
Thank you that is very good but I need a plain text version of it. I messed around last night with it and here is what I have so far. It still won't write to a file and the rest of the menu will not work without that. Any help is greatly appreciated
import java.util.*; import java.io.*; public class Test { public static void main(String[] args) throws IOException { Scanner myScanner = new Scanner(System.in); File file = new File("studentFile.txt"); BufferedWriter fileOut = new BufferedWriter(new FileWriter(file)); BufferedReader fileIn= new BufferedReader(new FileReader("studentFile.txt")); BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); //creating arrays int [][] score = new int[27][50]; int [] numberOfAssignments = new int[27]; String [] firstName = new String[27]; String [] lastName = new String[27]; int [] studentId = new int[27]; //declaring variables String end, assignmentTest, searchNumber, scoreTemp, studentIdTemp, empty, response; String restartTemp, numberOfStudentsString, assignmentIndexString, editEntryString; int numberOfStudentsTest; int i, h, l, p, y, s, entry; int firstNameCounter,arrayCounter, arrayCounter2, scoreCounter, scoreCounter2; int assignmentTestInt, test; int searchNumberInt, searchLoop, index; int editLoop, editEntry, assignmentIndex; int parseInt = 0; int scoreTempInt, restart, restartTest; int average, averageTemp, numberOfStudents, emptyFinder; empty= "empty"; end= "end"; //sets all the entrys in array score = -1 for later error checking. for(arrayCounter = 0; arrayCounter < 26; arrayCounter++) { for(scoreCounter= 0; scoreCounter < 50; scoreCounter++) { score[arrayCounter][scoreCounter] = -1; } } for(y=0;y<27;y++) { firstName[y]= "empty"; } numberOfStudentsTest= 0; for(arrayCounter2 = 0; arrayCounter2 < 26; arrayCounter2++) { studentId[arrayCounter2]= fileIn.read(); firstName[arrayCounter2]= fileIn.readLine(); lastName[arrayCounter2]= fileIn.readLine(); numberOfAssignments[arrayCounter2]= fileIn.read(); for(scoreCounter2= 0; scoreCounter2 < 50; scoreCounter2++) { score[arrayCounter2][scoreCounter2] = fileIn.read(); } } numberOfStudentsTest=fileIn.read(); restart=1; entry = 1; //creating menu while(entry > 0 && entry < 5) { System.out.println("*********"); System.out.println("Main Menu"); System.out.println("*********"); System.out.println("Option 1: Add a student"); System.out.println("Option 2: Modify a student"); System.out.println("Option 3: Delete a student"); System.out.println("Option 4: Run a report"); System.out.println("Option 5: End Program"); entry = myScanner.nextInt(); numberOfStudentsTest = 0; //start of the menu system. //the if statement works in conjunction with the menu //Menu Option 1 if(entry == 1) { System.out.println("Please enter the amount of students you wish to add"); numberOfStudentsString = dataIn.readLine(); numberOfStudents = Integer.parseInt(numberOfStudentsString); //This asks the user to enter the number of students they wish to add allowing them to effectivly end the loop with out the need to break it. for(i=(0); i < (numberOfStudents); i++) //The for loop is to run though it 26 times. It will also systematiclly fill in the arrays. { System.out.println("Please enter student's last name, or Enter end to quit:"); lastName[i]=(String) dataIn.readLine(); if (lastName[i].equals(end)) { break; } System.out.println("Please enter student first name, or Enter end to quit:"); firstName[i]=(String) dataIn.readLine(); if (firstName[i].equals(end)) { break; } System.out.println("Please enter the student ID number, or Enter -5 to quit:"); studentIdTemp = dataIn.readLine(); studentId[i]= Integer.parseInt(studentIdTemp); if(studentId[i] == -5) { break; } for (h = 0; h<25;h++) { System.out.println("Please enter student "+ (h+1) +" assignment score, Enter -5 to quit:"); assignmentTest = dataIn.readLine(); assignmentTestInt = Integer.parseInt(assignmentTest); if(assignmentTestInt == -5) { break; } else { score[i][h] = assignmentTestInt; numberOfAssignments[i] = h+1; } } numberOfStudentsTest= (numberOfStudentsTest+1); } } //Menu Option 2 if(entry == 2) { System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } index = searchLoop; System.out.println("Found Student"); System.out.println("Current Student Information"); System.out.println(studentId[index]); System.out.println(firstName[index]); System.out.println(lastName[index]); System.out.println(numberOfAssignments[index]); for(editLoop=0;editLoop < numberOfAssignments[index+1]; editLoop++) { System.out.print(score[index][editLoop]); } //Creating editing menu System.out.println("*********"); System.out.println("Edit Menu"); System.out.println("*********"); System.out.println("Option 1: Edit student ID"); System.out.println("Option 2: Edit First Name"); System.out.println("Option 3: Edit Last Name"); System.out.println("Option 4: Edit Scores"); editEntryString = dataIn.readLine(); editEntry = Integer.parseInt(editEntryString); if(editEntry == 1)//input option 1 { System.out.println("Please enter the new student ID"); studentIdTemp= dataIn.readLine(); studentId[index]= Integer.parseInt(studentIdTemp); } if(editEntry == 2)//input option 2 { System.out.println("Please enter the new first name of the student"); firstName[index] = dataIn.readLine(); } if(editEntry == 3 )//input option 3 { System.out.println("Please enter the new last name of the student"); lastName[index] = dataIn.readLine(); } if(editEntry == 4)//input option 4 { System.out.println("Please enter the number of the assignment you would like to edit"); assignmentIndexString = dataIn.readLine(); assignmentIndex = Integer.parseInt(assignmentIndexString); assignmentIndex = (assignmentIndex - 1); System.out.println("Please enter the new score for the assignment"); score[index][assignmentIndex]=dataIn.read(); } } //Menu Option 3 if(entry == 3) { System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } //replace with blank spaces and -1 where appropriate index = searchLoop; firstName[index] = " "; lastName[index] = " "; studentId[index] = -1; numberOfAssignments[index] = -1; for(p=0;p<25;p++) { score[index][p]= -1; } } //Menu Option 4 if(entry == 4) { System.out.println("Who do you wish to run the report for?"); System.out.println("Please enter the students ID"); searchNumber = dataIn.readLine(); searchNumberInt = Integer.parseInt(searchNumber); for(searchLoop=0; searchLoop < 26; searchLoop++) if(searchNumberInt == studentId[searchLoop]) { index = searchLoop; break; } index = searchLoop; System.out.println(firstName[index]); System.out.println(lastName[index]); System.out.println(studentId[index]); System.out.println(numberOfAssignments[index]); for(int q=0;q<numberOfAssignments[index];q++) { System.out.print(score[index][q] + " "); } averageTemp = 0; for(int j=0;j < numberOfAssignments[index];j++) { averageTemp= averageTemp + score[index][j]; } average=0; //Seems to just skip over this section entirely. I have no idea why. average = averageTemp/numberOfAssignments[index]; System.out.println(average); } //Menu Option 5 if(entry == 5) { System.exit(0); } System.out.println("Would you like to restart the program? If you wish to restart enter the number 4"); restartTemp= dataIn.readLine(); restartTest = Integer.parseInt(restartTemp); restart= restartTest; }//close while loop for main menu for(test=0;test<50;test++) { fileOut.write(studentId[test]); fileOut.write(firstName[test]); fileOut.write(lastName[test]); fileOut.write(numberOfAssignments[test]); for(l=0;l<50;l++) { fileOut.write(score[test][l]); } } fileOut.write(numberOfStudentsTest); fileOut.close(); } }