0

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]);
        }
}
2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

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)

0

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.

0

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.

0

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);

          }
}

Edited by Krazykado: n/a

0

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";

0

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);

          }
}
0

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 = ("");

Edited by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.