Hello, I am having a problem putting this Array's output in a single GUI window. When I receive the output it just shows the grades in individual GUI windows (IE Erik 98 [OK], Fred 76 [OK], etc). If I put the output outside the loop then it doesn't recognize grade. I'm at a loss right now. I would like to have the output of all grades in one GUI window. If anybody could help it would be much appreciated. Thank you. I'm here to learn.

import java.util.Scanner;
import java.lang.String;
import javax.swing.*;

public class SortingStudentsGUI {

        public static void main(String[] args) {

                //Prompt user to enter number of students;
                String NumberStudentsString = JOptionPane.showInputDialog(
                	"Enter the number of students: ");
                int num = Integer.parseInt(NumberStudentsString);


                //Create arrays
                String[] names = new String[num];
                double[] grades = new double[num];

                // Fill arrays with prompted user data
                for(int i = 0; i < num; i++)
                {
                  String StudentNameString = JOptionPane.showInputDialog(
                  	"Enter the name of the student: ");
                  names[i] = (StudentNameString);
                  String StudentScoreString = JOptionPane.showInputDialog(
                  	"Enter the score of the student: ");
                  grades[i] = Double.parseDouble(StudentScoreString);
                }


                for (int bound = num-1; bound > 0; bound--)
                {
                        for (int i = 0; i < bound; i++)
                        {
                                // Grades is the sorting criteria
                                if (grades[i] < grades[i+1])
                                {
                                        // Swap both grades and names
                                        double temp1 = grades[i+1];
                                        grades[i+1] = grades[i];
                                        grades[i] = temp1;

                                        String temp2 = names[i+1];
                                        names[i+1] = names[i];
                                        names[i] = temp2;

                                }
                        }
                }

                //*****************************************
                // Display sorted names and grades
                //*****************************************

                for (int i = 0; i < num; i++)
                        JOptionPane.showMessageDialog(null, names[i]+ "   " + grades[i]);
        }
}

Recommended Answers

All 7 Replies

You could concatenate all the names & grades into one String, with \n chars after each line, then display that String

names[1]+ " " + grades[1] + "\n" + names[2]+ " " + grades[2] + "\n" + ... etc (but build it in a loop, obviously)

Tried building a loop for it but I guess I don't know where to start because I'm getting the same outcome. I'm not very advanced as you can see. Still trying to learn as I go. If you don't mind explaining a little more please. Thank you.

Start with an empty String (""). In the loop append all the names/scores/newline characters to it. After the loop display it in your option pane.
If that still isn't working for you, post the relevant part of your updated code.

This is the error I'm getting now.
C:\Users\Erik\Desktop\CMIS141\SortingStudentsGUI.java:57: not a statement
String output = (names + " " + grades + "\n");
^
C:\Users\Erik\Desktop\CMIS141\SortingStudentsGUI.java:57: ';' expected
String output = (names + " " + grades + "\n");
^
2 errors


//*****************************************
                // Display sorted names and grades
                //*****************************************

                  for (int i = 0; i < num; i++)
                      String output = (names[i] + "  " + grades[i] + "\n");

                   JOptionPane.showMessageDialog(null, output);

          }
}

Suggest you re-read my previous post:
Start with an empty String ("").
In the loop append all the names/scores/newline characters to it.
After the loop display it in your option pane.

You append something to a String with syntax like:
myString = myString + " some new text " + "\n";

Thank you for the help and patience. Sorry didn't catch it the first time around. Here is what I did and it work.

//*****************************************
                // Display sorted names and grades
                //*****************************************
				  String myString = ("");
                  for (int i = 0; i < num; i++)
                      myString = myString + (names[i] + "  " + grades[i] + "\n");

                   JOptionPane.showMessageDialog(null, myString);

          }
}

Excellent! Please mark this thread as "solved" now - if you have a new problem later you can start a new thread.

ps: One very small observation:
You don't need the () round the expressions after the = in statements like
names = (StudentNameString);
String myString = ("");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.