Traplord
Traplord

Reputation: 101

How do I print two 2D arrays side by side?

So a user enters his/her first and last name in different fields. (has to be that way)

image for ref: enter image description here

What I am trying to do is display their name beside their 4 test marks.

So it would look like this

John Smith 55.0 100.0 23.0 50. Jane Smith 100.0 50.0 76.0 22.0 so on and so on.

My code is

public class StudentGradesView extends FrameView {
    final static int students=15;
    final static int numOfTest=4;
    final static int firstNLast=2;

    //Start with the first student in the 2D array
    private int row = 0;


    //DELCARING THE 2D ARRAY
    double[][] marks = new double[students][numOfTest];
    String[][] names = new String[students][firstNLast];

    //1D Arrays
    double testScores[]=new double[4];
    String studentNames[]=new String[2];


    public void addInfo(){
        studentNames[0]= firstNameIn.getText();
        studentNames[1]= lastNameIn.getText();

        testScores[0]=Double.parseDouble(testOneIn.getText());
        testScores[1]=Double.parseDouble(testTwoIn.getText());
        testScores[2]=Double.parseDouble(testThreeIn.getText());
        testScores[3]=Double.parseDouble(testFourIn.getText());

        //Add first and last name to 2d array
        for(int col=0;col <studentNames.length; col++){
            names[row][col]= studentNames[col];
        }

        //Add all four test scores to the current student
        for(int col=0;col < testScores.length; col++){
            marks[row][col]= testScores[col];
        }
        //Advance to the next student
        row++;
    }

    public void displayArray(){
        for(int i=0;i<names.length;i++){
            for(int j=0; j<firstNLast;j++){
                finalOutput.append(names[i][j]+" ");
            }
            finalOutput.append("\n");
        }

        for(int i=0; i<marks.length;i++){
            for(int j=0; j <numOfTest; j++){
                finalOutput.append(marks[i][j]+" ");
            }
            finalOutput.append("\n");
        }
    }
}

What I end up getting when I hit the list/displayArray(); is this: Shows the names separately from the numbers.

Like this here

So I am stuck on how to make them print beside eachother. Also is there a way for it to only print the number of entries the user has entered? like only print one line if one user has entered 1 set of information?

Thanks.

Upvotes: 1

Views: 2148

Answers (3)

Stephen C
Stephen C

Reputation: 719436

Because this is obviously a learning exercise, I'm not going to give you the code.

The quick and dirty answer is to change your display code to something like this:

public void displayArray(){
    for (int i = 0; i < names.length; i++) {
        if (/* some test to check that the entry is populated */) {
            // append the names for student i
            // append the scores for student i 
            // append newline 
        }
    }
}

I'll leave it to you to fill in the details.


But the real problem here is that you have made a number of design mistakes.

  1. Storing the names and scores as arrays of arrays is a bad idea. Java provides List types that are easier to use, and do not have a fixed length.

    • You don't need to have a fixed number of scores per student.

    • You don't need to have a fixed number of names per student.

    • You don't need to have a fixed number of students.

  2. You should create a class to represent each Student. It could be really simple, consisting of just getters and setters for the name and score lists, or you could attempt to encapsulate the state ...

  3. Once you have create a class for Student, you could use JList (or JTable) to display the List<Student>. The display will look nicer, and you won't need a "list" button.

(Now it is possible, that these things will be addressed by your lecturer ... and that the exercise after this one involves implementing these things ...)

Upvotes: 2

mrres1
mrres1

Reputation: 1155

If the arrays are parallel then you can use one loop:

public void displayArray()
{
    for(int i = 0 ; i < names.length ; i++)
    {
        /*
         *  If the first or last name are null then skip over and go to the 
         *      next item
         */
        if(names[i][0] == null || names[i][1] == null)
        {
            continue;
        }

        for(int j = 0; j < firstNLast; j++)
        {
            finalOutput.append(
                String.format("%10s%s%10s%s", 
                        names[i][j], " ", marks[i][j], " "));
        }

        finalOutput.append("\n");
    }
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

In your displayArray method, I would first clear the JTextArea by calling finalOutput.setText(""), and then in the for loop go from 0 to rows -- the number of students that have been added so far. Otherwise you'll loop through the whole array which will have all nulls above the row index.

Better -- don't use Arrays but rather ArrayLists.

Better still: create a Student class that holds an individual student's names and grades, and use a single ArrayList<Student>.

Even better still -- display the data above in a JTable.

Upvotes: 3

Related Questions