I'm trying to create a program using a two-dimensional array to store information about students, their first and last name then 4 grades.
The user has to be able to enter all of that into the program and press a button to calculate the averages of the courses. They should also be able to enter the first and last name of a student and have it output their average grade. The program should also be able to list all information stored in it.
I realize that it would probably be easier to use arraylists but this is for a course and it specifies that I have to use a two-dimensional array.
The development environment we are using is NetBeans 6.5(6.8 is the last version that retains the design window). Yes I realize that this is severely outdated but that's what we have to use. This version has a design window so we don't have to worry about any of the GUI coding, we just drag in the labels and text areas and buttons we need. It has to be able to handle up to 15 students. I'll try to explain my thoughts and theory behind the code as it goes on.
//Create and intialize array to have 15 rows and 6 columns.
String [][] records = new String [15][6];
int n = 1;
double clicked = 0;
private void addActionPerformed(java.awt.event.ActionEvent evt) {
//The basic idea behind the add method here is that we take the input from the user and assign it to the array by taking the number of times this method has been used as a reference for where it should go in the array. This method actually works fine, I just put in here for reference in case someone thinks it might be important.
String nameFirst,nameLast, grade1,grade2, grade3, grade4;
nameFirst = firstName.getText();
nameLast = lastName.getText();
grade1 = class1Grade.getText();
grade2 = class2Grade.getText();
grade3 = class3Grade.getText();
grade4 = class4Grade.getText();
records[n-1][0] = nameFirst;
records[n-1][1] = nameLast;
records[n-1][2] = grade1;
records[n-1][3] = grade2;
records[n-1][4] = grade3;
records[n-1][5] = grade4;
output.append(records[n-1][0] + "\n");
output.append(records[n-1][1] + "\n");
output.append(records[n-1][2] + "\n");
output.append(records[n-1][3] + "\n");
output.append(records[n-1][4] + "\n");
output.append(records[n-1][5] + "\n");
n++;
firstName.setText("");
lastName.setText("");
class1Grade.setText("");
class2Grade.setText("");
class3Grade.setText("");
class4Grade.setText("");
clicked++;
}
private void listActionPerformed(java.awt.event.ActionEvent evt) {
//This is the code that we were shown to display all elements in the array but for whatever reason it doesn't want to go past row 0(first row) and I don't know why.
for (int row = 0; row <= 16; row++) {
output.append(row + "\n");
for( int col=0; col <= 6; col++) {
output.append(records[row][col] +"\n");
}
}
//This is the code I added in to make sure that the add method was actually putting things where they belonged. It is but it did show that the above loop wasn't working.
//output.append(records[0][0] + "\n");
//output.append(records[0][1] + "\n");
//output.append(records[0][2] + "\n");
//output.append(records[0][3] + "\n");
//output.append(records[0][4] + "\n");
//output.append(records[0][5] + "\n");
//output.append(records[1][0] + "\n");
//output.append(records[1][1] + "\n");
//output.append(records[1][2] + "\n");
//output.append(records[1][3] + "\n");
//output.append(records[1][4] + "\n");
//output.append(records[1][5] + "\n");
}
private void calculateCourseAverageActionPerformed(java.awt.event.ActionEvent evt) {
double total1 = 0, total2 = 0, total3 = 0, total4 =0;
double col2, col3, col4, col5;
//col2 = Double.parseDouble(records[0][2]);
//Here I try to add up all the stored values in a particular column. Originally it kept giving back a floating decimal error. It turned out the the value in the array was being multiplied by 64(4 from first loop, 16 from second, 16*4=64). Now its only being multiplied by 16, not much better but an improvement none the less.
for(int col = 2; col<=5; col++){
for(int row = 0; row<=15; row++){
if(col == 2){
col2 = Double.parseDouble(records[0][2]);
total1 = total1 + col2;
}
if(col == 3){
col3 = Double.parseDouble(records[0][3]);
total2 = total2 + col3;
}
if(col == 4){
col4 = Double.parseDouble(records[0][4]);
total3 = total3 + col4;
}
if(col == 5){
col5 = Double.parseDouble(records[0][5]);
total4 = total4 + col5;}
}
}
output.append("The average for course 1 is " + total1/4);
output.append("The average for course 2 is " + total2/4);
output.append("The average for course 3 is " + total3/4);
output.append("The average for course 4 is " + total4/4);
}
private void calculateStudentAverageActionPerformed(java.awt.event.ActionEvent evt) {
String nameFirst, nameLast;
double studentAverage, total = 0, grade, finalGrade;
boolean continuation;
int student = 0;
//here we take the first name and last name(that the user should input). The method is supposed to check through the array for the first name and if it finds it it should check the last name then add up all the number is that particular row. Tests show that it seems to work fine until the Name1 Name2 area(*), it shows that the records[row][0] and records[row][1] are equal to null which negates the rest of my code here.
nameFirst = firstName.getText();
nameLast = lastName.getText();
output.append("Name check " + nameFirst + nameLast + "\n");
while(continuation = true){
for(int row=0;row<=15;row++){
for(int col=0;col<=0;col++){
*
output.append("Name1 " + records[row][0] + "\n");
output.append("Name2 " + records[row][1] + "\n");
*
if(records[row][0].equals(nameFirst)){
if(records[row][1].equals(nameLast)){
continuation = false;
student = row;
String test = Integer.toString(student);
output.append("Student row # " + test + "\n");
}
}
}
}
}
for(int col = 2; col<=5; col++){
grade = Double.parseDouble(records[student][col]);
output.append("Grade " + grade + "\n");
total = total + grade;
output.append("Total " + total + "\n");
}
finalGrade = total / 4;
output.append(finalGrade + "\n");
output.append("First Name " + records[student][0] + "\n");
output.append("Last Name " + records[student][1] + "\n");
output.append(records[student][0] + records[student][1] + "'s average is " + finalGrade);
continuation = true;
}
Any help or ideas, thoughts, etc... would be very much appreciated. I have been stuck on this and a matching game assignment for the past month and I figured it was about time I asked for someone elses input and suggestions.