This code keeps giving me the error: Exception in thread \"main\" java.lang.Arra
ID: 3743864 • Letter: T
Question
This code keeps giving me the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at fscgradebook.FSCgradebook.addRecord(FSCgradebook.java:54)
at fscgradebook.FSCgradebook.main(FSCgradebook.java:145)
/Users/john/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Why and how do i fix it?
Here is the code:
package fscgradebook;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
* @author john
*/
public class FSCgradebook {
//
// METHODS USED TO PROCESS THE COMMANDS IN THE INPUT FILE.
// - All we really do is print a small message for each command
//
// Method to Proccess ADDRECORD
public static int addRecord(Scanner input, PrintWriter output, Student[] students) {
int id = input.nextInt();
String fname = input.next();
String lname = input.next();
int g1 = input.nextInt();
int g2 = input.nextInt();
int g3 = input.nextInt();
int index = -1;
Student s = new Student(fname, lname, id, g1, g2, g3, (((g2 * (0.3)) + (g3 * (0.4)) + (g1 * (0.3)))));
// save new student into array
students[0] = s;
output.println("Command: ADDRECORD");
output.println(fname + " " + lname + " (ID# " + id + ")" + " has been added to the FSC Grade Book");
output.println(" His final grade is " + (((g2 * (0.3)) + (g3 * (0.4)) + (g1 * (0.3)))) + "(" + ")");
for (int i = 0; i < Student.getNumStudents(); i++) { // < instead of <=, don't hardcode the length
if (Student.getNumStudents() == 0) {
students[0] = s;
Student.increaseStudents();
return 0; // once we insert a, stop looping
}
for (int y =0; y < Student.getNumStudents(); y++)
if (id < students[y].getID()){
index = y;
}
}
if (index != 1){
for(int i = Student.getNumStudents()-1; i > index; i--){
students[i + 1] = students[i];
}
students[index] = s;
}
else {
students[Student.getNumStudents()] = s ;
}
Student.increaseStudents();
return index;
}
// Method to Proccess SEARCHBYID
public static void searchbyID(PrintWriter output) {
output.println("Command SEARCHBYID");
//if (Student.ID) == ) {
}
// Method to Proccess SEARCHBYNAME
public static void searchbyNAME(PrintWriter output) {
output.println("Command: SEARCHBYNAME");
}
// Method to Proccess DISPLAYSTATS
public static void displaySTATS(PrintWriter output) {
output.println("Command: DISPLAYSTATS");
//output.println("Statistical Results of"+course+"(Instructor: Dr "+instructor_fName+" "+instructor_lName+"):");
output.println(" Total number of student records: ");
output.println(" Average Score: ");
output.println(" Highest Score: ");
output.println(" Lowest Score: ");
output.println(" Total 'A' Grades: " + "(" + "% of class)");
output.println(" Total 'B' Grades: " + "(" + "% of class)");
output.println(" Total 'C' Grades: " + "(" + "% of class)");
output.println(" Total 'D' Grades: " + "(" + "% of class)");
output.println(" Total 'F' Grades: " + "(" + "% of class)");
}
// Method to Proccess DISPLAYSTUDENTS
public static void displaySTUDENTS(PrintWriter output) {
output.println("Command: DISPLAYSTUDENTS");
}
// Method to Proccess quit
public static void QUIT(PrintWriter output) {
output.println("Thank you for using the FSC Grade Book.");
output.println("Goodbye.");
}
//*************//
// MAIN METHOD //
//*************//
public static void main(String[] args) throws Exception {
// Variables needed for program
String command; // used to save the command read from input file
//Student x = new Student();
//x.setFname("yh7yh6t");
//System.out.println(x.getFname());
// OPEN FILES
// Input File:
File inputFile = new File("FSCgradebook.in");
if (!inputFile.exists()) {
System.out.println("Input file, " + inputFile + ", does not exist.");
System.exit(0);
}
// Output File:
File outputFile = new File("FSCgradebook.out");
// Make Scanner variable to read from input file, and
// make Printwriter variable to print to output
Scanner input = new Scanner(inputFile);
PrintWriter output = new PrintWriter(outputFile);
String course = input.nextLine();
String instructor_fName = input.next();
String instructor_lName = input.next();
int maxNumberStudents = input.nextInt();
Student[] students = new Student[maxNumberStudents];
output.println("Welcome to the FSC Grade Book.");
do {
command = input.next();
// ADDRECORD
if (command.equals("ADDRECORD") == true) {
addRecord(input, output, students);
} // *NOTE:
// Notice, that we send the "output" variable (the PrintWriter variable) to the
// method. This allows us to print to the file from inside the method.
// You can also send the "input" variable to the method, which would allow you to
// read from the file from inside the method also.
// COMMAND2
else if (command.equals("SEARCHBYID") == true) {
searchbyID(output);
} // COMMAND3
else if (command.equals("SEARCHBYNAME") == true) {
searchbyNAME(output);
} else if (command.equals("DISPLAYSTATS") == true) {
displaySTATS(output);
} else if (command.equals("DISPLAYSTUDENTS") == true) {
displaySTUDENTS(output);
} // Command QUIT: Quit the Program
else if (command.equals("QUIT") == true) {
output.println("Thank you for using the FSC Grade Book.");
output.println("Goodbye.");
} // Invalid Command
else {
System.out.println("Invalid Command: input invalid.");
}
} while (command.equals("QUIT") != true);
// Close input and output
input.close();
output.close();
}
}
Here is the student class:
package fscgradebook;
/**
*
* @author john
*/
public class Student {
private String fname;
private String lname;
int ID;
private int[] examGrades;
private double finalGrade;
private char letterGrade;
private static int numStudents = 0;
private int maxNumStudents;
public Student() {
}
Student[] students = new Student[maxNumStudents];
public Student(String fname, String lname, int ID, int g1, int g2, int g3, double finalGrade) {
this.fname = fname;
this.lname = lname;
this.ID = ID;
this.finalGrade = finalGrade;
examGrades = new int[3];
examGrades[0] = g1;
examGrades[1] = g2;
examGrades[2] = g3;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public double getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(double finalGrade) {
this.finalGrade = finalGrade;
}
public char getLetterGrade() {
return letterGrade;
}
public void setLetterGrade(char letterGrade) {
this.letterGrade = letterGrade;
}
public static int getNumStudents() {
return numStudents;
}
public static void setNumStudents(int numStudents) {
Student.numStudents = numStudents;
}
public static void increaseStudents(){
numStudents++;
}
}
Explanation / Answer
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
* @author john
*/
public class FSCgradebook {
//
// METHODS USED TO PROCESS THE COMMANDS IN THE INPUT FILE.
// - All we really do is print a small message for each command
//
// Method to Proccess ADDRECORD
public static int addRecord(Scanner input, PrintWriter output,
Student[] students) {
int id = input.nextInt();
String fname = input.next();
String lname = input.next();
int g1 = input.nextInt();
int g2 = input.nextInt();
int g3 = input.nextInt();
int index = -1;
Student s = new Student(fname, lname, id, g1, g2, g3,
(((g2 * (0.3)) + (g3 * (0.4)) + (g1 * (0.3)))));
// save new student into array
students[0] = s;
output.println("Command: ADDRECORD");
output.println(fname + " " + lname + " (ID# " + id + ")"
+ " has been added to the FSC Grade Book");
output.println(" His final grade is " +
(((g2 * (0.3)) + (g3 * (0.4)) + (g1 * (0.3)))) + "("
+ ")");
for (int i = 0; i < Student.getNumStudents();
i++) { // < instead of <=, don't hardcode the length
if (Student.getNumStudents() == 0) {
students[0] = s;
Student.increaseStudents();
return 0; // once we insert a, stop looping
}
for (int y = 0; y < Student.getNumStudents(); y++)
if (id < students[y].getID()) {
index = y;
}
}
if (index != -1) {
for (int i = Student.getNumStudents() - 1; i > index; i--) {
students[i + 1] = students[i];
}
students[index] = s;
} else {
index = Student.getNumStudents();
if(index<Student.length){
students[index] = s;
Student.increaseStudents();
}
else{
System.out.println("Adding student not possible, it reached to max size");
return -1;
}
}
return index;
}
// Method to Proccess SEARCHBYID
public static void searchbyID(PrintWriter output) {
output.println("Command SEARCHBYID");
// if (Student.ID) == ) {
}
// Method to Proccess SEARCHBYNAME
public static void searchbyNAME(PrintWriter output) {
output.println("Command: SEARCHBYNAME");
}
// Method to Proccess DISPLAYSTATS
public static void displaySTATS(PrintWriter output) {
output.println("Command: DISPLAYSTATS");
// output.println("Statistical Results of"+course+"(Instructor: Dr
// "+instructor_fName+" "+instructor_lName+"):");
output.println(" Total number of student records: ");
output.println(" Average Score: ");
output.println(" Highest Score: ");
output.println(" Lowest Score: ");
output.println(" Total 'A' Grades: "
+ "("
+ "% of class)");
output.println(" Total 'B' Grades: "
+ "("
+ "% of class)");
output.println(" Total 'C' Grades: "
+ "("
+ "% of class)");
output.println(" Total 'D' Grades: "
+ "("
+ "% of class)");
output.println(" Total 'F' Grades: "
+ "("
+ "% of class)");
}
// Method to Proccess DISPLAYSTUDENTS
public static void displaySTUDENTS(PrintWriter output) {
output.println("Command: DISPLAYSTUDENTS");
}
// Method to Proccess quit
public static void QUIT(PrintWriter output) {
output.println("Thank you for using the FSC Grade Book.");
output.println("Goodbye.");
}
// *************//
// MAIN METHOD //
// *************//
public static void main(String[] args) throws Exception {
// Variables needed for program
String command; // used to save the command read from input file
// Student x = new Student();
// x.setFname("yh7yh6t");
// System.out.println(x.getFname());
// OPEN FILES
// Input File:
File inputFile = new File("FSCgradebook.in");
if (!inputFile.exists()) {
System.out.println("Input file, " + inputFile + ", does not exist.");
System.exit(0);
}
// Output File:
File outputFile = new File("FSCgradebook.out");
// Make Scanner variable to read from input file, and
// make Printwriter variable to print to output
Scanner input = new Scanner(inputFile);
PrintWriter output = new PrintWriter(outputFile);
String course = input.nextLine();
String instructor_fName = input.next();
String instructor_lName = input.next();
int maxNumberStudents = input.nextInt();
Student[] students = new Student[maxNumberStudents];
output.println("Welcome to the FSC Grade Book.");
do {
command = input.next();
// ADDRECORD
if (command.equals("ADDRECORD") == true) {
addRecord(input, output, students);
} // *NOTE:
// Notice, that we send the "output" variable (the PrintWriter variable)
// to the
// method. This allows us to print to the file from inside the method.
// You can also send the "input" variable to the method, which would allow
// you to
// read from the file from inside the method also.
// COMMAND2
else if (command.equals("SEARCHBYID") == true) {
searchbyID(output);
} // COMMAND3
else if (command.equals("SEARCHBYNAME") == true) {
searchbyNAME(output);
} else if (command.equals("DISPLAYSTATS") == true) {
displaySTATS(output);
} else if (command.equals("DISPLAYSTUDENTS") == true) {
displaySTUDENTS(output);
} // Command QUIT: Quit the Program
else if (command.equals("QUIT") == true) {
output.println("Thank you for using the FSC Grade Book.");
output.println("Goodbye.");
} // Invalid Command
else {
System.out.println("Invalid Command: input invalid.");
}
} while (command.equals("QUIT") != true);
// Close input and output
input.close();
output.close();
}
}
I changed lines in addRecord() method. If you still getting error, give me comment, I will help...