I created a String[][] grades, who's size is set by number of students User enters.

The array is then populated with my first for loop, and the next for loop is supposed to print out each students date line by line.

If I only enter 1 Student and fill in the data, it prints out perfectly.

But if I enter 2 or more students, things go wrong:

  1. It prints the information twice side by side, and
  2. It only prints the last entered data, rather than e.g. Jane & Jo's data

Could someone take a look at my two for loops and see if they can spot what I am doing wrong please.

import javax.swing.JOptionPane;
import javax.swing.*;
public class StudentGrades {
  static double percentage;
  static char grade;
  public static void main (String[] args) {
    String name = "", temp;
    int students, count = 0, quiz1 = 0, quiz2 = 0, midterm_exam = 0, final_exam = 0;

    temp = JOptionPane.showInputDialog(null, "How many students in class?");
    students = Integer.parseInt(temp);

    String[][] grades = new String[students][5];

    while (students > count) {
      name = JOptionPane.showInputDialog(null, "Enter Student Name");
      temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Quiz 1 score out of 10");
      quiz1 = Integer.parseInt(temp);
      temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Quiz 2 score out of 10");
      quiz2 = Integer.parseInt(temp);
      temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Midterm Exam score out of 100");
      midterm_exam = Integer.parseInt(temp);
      temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Final Exam score out of 100");    
      final_exam = Integer.parseInt(temp);

      count++;

      for (int a = 0; a < grades.length; a++) {
        grades[a][0] = name;
        grades[a][1] = String.valueOf(quiz1);
        grades[a][2] = String.valueOf(quiz2);
        grades[a][3] = String.valueOf(midterm_exam);
        grades[a][4] = String.valueOf(final_exam);
      }
    } 
    System.out.println("Name\tQuiz 1\tQuiz2\tMidterm\tFinal\tPercentage\tGrade");
    for (int a = 0; a < grades.length; a++) {
      for (int b = 0; b < grades.length; b++) {
        grades[a][0] = name;
        grades[a][1] = String.valueOf(quiz1);
        grades[a][2] = String.valueOf(quiz2);
        grades[a][3] = String.valueOf(midterm_exam);
        grades[a][4] = String.valueOf(final_exam);

        System.out.print(
          grades[a][0] + "\t" +
          grades[a][1] + "\t" +
          grades[a][2] + "\t" +
          grades[a][3] + "\t" +
          grades[a][4] + "\t" +
          percentage(quiz1, quiz2, midterm_exam, final_exam) + "%\t\t" +
          grade(percentage)
        );
      }
      System.out.println("\n");
    }
  }
  public static double percentage(int i, int j, int k, int l) {
    percentage = (
      (i * 1.25) +
      (j * 1.25) +
      (k * 0.25) +
      (l * 0.50)
    );
    return percentage;
  }
  public static char grade(double p) {
    p = percentage;

    if (p >= 90) {
      grade = 'A';
    }
    else if (p >= 80 && grade < 90) {
      grade = 'B';
    }
    else if (p >= 70 && grade < 80) {
      grade = 'C';
    }
    else if (p >= 60 && grade < 70) {
      grade = 'D';
    }
    else {
      grade = 'F';
    }
    return grade;
  }
}

Lines 28-35 you put the data into every row of the array, rather than just into the current row

then in 38-43 you overwrite your values again when you should be just printing them.

Edited 3 Years Ago by JamesCherrill

your first error is in that loop:

for (int b = 0; b < grades.length; b++) { .. }

remove that, you don't need it. also: don't set the values in the array, extract them instead. at this point, you are overwriting the values in your array.

your second error: in reading the info: get rid of that for- loop. the while (students > count ) is all you need. in the assignments to your array, replace the a's by count as index of the array, and after that, move the count++; increment till after the values are stored in the array.

once you'll do that, you'll notice that this:
percentage(quiz1, quiz2, midterm_exam, final_exam)

gives a wrong result. use the values from the grade array there.
so: pass the values like Integer.parseInt(grades[a][1]); without first overwriting those values, as mentioned before.

the best thing to do is seperate the blocks: create a method to read the data,
create a method to display it.

that way, it's easier to maintain and correct: a simple input method is quicker to check compared to the entire code.

the code itself isn't bad, you're struggling with logical errors here, and there's no way any IDE can warn you for any of these.

Edited 3 Years Ago by stultuske: corrected (serious ) typo's

Your second (output) loop has an inner loop that looks to have been intended to iterate through the five test values; however, you then unrolled that loop, making the inner loop unnecessary. The solution is either to eliminate that inner loop altogether:

    for (int a = 0; a < grades.length; a++) {
        System.out.print(
          grades[a][0] + "\t" +
          grades[a][1] + "\t" +
          grades[a][2] + "\t" +
          grades[a][3] + "\t" +
          grades[a][4] + "\t" +
          percentage(quiz1, quiz2, midterm_exam, final_exam) + "%\t\t" +
          grade(percentage)
        );
      System.out.println("\n");
    }

or try to fit the unrolled values back into the loop (note the subscript before the .length property):

    System.out.println("Name\tQuiz 1\tQuiz2\tMidterm\tFinal\tPercentage\tGrade");
    for (int a = 0; a < grades.length; a++) {
      for (int b = 0; b < grades[a].length; b++) {
        System.out.print(grades[a][b] + "\t");
      }
      System.out.print(
        percentage(quiz1, quiz2, midterm_exam, final_exam) + "%\t\t" +
        grade(percentage) + "\n"
      );
    } 

Note that this does not address the problems with the first loop.

Thank you to everyone for your thoughts. James you were the first to note my mistake of over writing my own data. Thanks for that. Stultuske, you idea about losing my for loop and using the while (students > count) was brilliant :) Also, thanks for explaining the details of what to do. Schol-R-Lea, thanks for your suggestions on my second loop; it helped me reason out my faulty logic. I took the popular advice to lose that inner loop. It was not needed as you noted. James (moderator), I was not sure who your post was to, me or someone else who replied. I did mean, in my title: I was having trouble with how to display the content of my array.

Ooops, sorry stultuske about the flag on post. I took it off now. Sorry I thought that was for giving a thumbs up for a great answer.

Edited 3 Years Ago by JoeD1

James (moderator), I was not sure who your post was to, me or someone else who replied.

No, that referred to stulkusk's immediately preceeding post (now corrected) where he inadvertently wrote "array" when he meant "method". :)
J

This question has already been answered. Start a new discussion instead.