0

I am trying to create a program with an array that allows a user to input their students names and 4 test marks to be put into a grade book.

However, the code below only catches the Name and not the grades.

How do I convert test1-4 into an integer? I have tried Integer.parseInt() but no luck.

Any help would be appreciated.

public class StudentGrades extends javax.swing.JFrame {
    private String[][] grades = new String[15][5];
    private int global = 0;

    public StudentGrades() {
        initComponents();
    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
        //Clear output area for new input
        gradesOutput.setText("");

        //Declare Variables
        String firstName, lastName, name;
        String test1, test2, test3, test4;

        test1 = t1Input.getText();
        test2 = t2Input.getText();
        test3 = t3Input.getText();
        test4 = t4Input.getText();

        firstName = firstInput.getText();
        lastName = lastInput.getText();
        name = firstName + " " + lastName;

        name = grades[global][0];
        test1 = grades[global][1];
        test2 = grades[global][2];
        test3 = grades[global][3];
        test4 = grades[global][4];

        global++;
    }

    private void listInputActionPerformed(java.awt.event.ActionEvent evt) {
        StringBuilder sb = new StringBuilder();
        for (int row = global; row <= 14; row++) {
            for (int col = global; col <= 4; col++) {
                sb.append(grades[row][col]);
                sb.append(", ");
            }
            sb.append("\n");
            gradesOutput.setText(sb.toString());
        }
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
}                   
INFINITY
  • 41
  • 6

1 Answers1

0
int grade1 = Integer.parseInt(test1);

should work. Do you get an exception if you try it? If so, which one?

Also I'm not sure but shouldn't

name = grades[global][0];
test1 = grades[global][1];
test2 = grades[global][2];
test3 = grades[global][3];
test4 = grades[global][4];

be

grades[global][0] = name;
grades[global][1] = test1;
grades[global][2] = test2;
grades[global][3] = test3;
grades[global][4] = test4;

since you're trying to access these variables but never wrote them into the array, I'm assuming you want to store them here.

Kilian Balter
  • 43
  • 1
  • 5
  • When I flip around the name with the grades it stays the same. I am getting an output however: "null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null," I am getting the "Int cannot be converted to String" error. – INFINITY Jun 01 '21 at 00:54