I just needed some assistance on formatting the output into a specific fashion. I satisfied all the requirements of the assignment, except the output.

I apologize for the bad format of this post, any suggestions and help is greatly appreciated.

I need the output to look like:
Mr. Random - $500
Mr. BobRandom - $250
etc-8 more times
(sum total of all dollars)

Heres my code:

import javax.swing.JOptionPane;
import java.util.Arrays;

public class PeerTutors {
    public static void main(String[] args) {
        String[] myNameArray = new String[2];
        String[] myGradeArray = new String[2];
        double[] myStudentArray = new double[2];
        double[] stipend = new double[2];
        int sum = 0;

        for (int i = 0; i < 2; i++) {
            myNameArray[i] = (JOptionPane
                    .showInputDialog(null,
                            "Enter the Peer Tutor name, last name then followed by the first name."));

        }
        Arrays.sort(myNameArray, String.CASE_INSENSITIVE_ORDER);
        JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
                + Arrays.toString(myNameArray));

        for (int a = 0; a < 2; a++) {
            myGradeArray[a] = JOptionPane.showInputDialog(null,
                    "Please enter the highest degree earned for: "
                            + myNameArray[a]);
        }
        JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
                + Arrays.toString(myNameArray) + Arrays.toString(myGradeArray));

        for (int b = 0; b < 2; b++) {
            myStudentArray[b] = Double.parseDouble(JOptionPane.showInputDialog(
                    null, "Please enter the number of students"
                            + myNameArray[b] + " has assisted"));
        }
        JOptionPane.showMessageDialog(null,
                "The Peer Tutors Report: " + Arrays.toString(myNameArray)
                        + "with" + Arrays.toString(myStudentArray)
                        + " of children tutoring");

        for (int c = 0; c < 2; c++) {
            if (myGradeArray[c].equals("BS")) {
                stipend[c] = 9.5 * myStudentArray[c];

            } else if (myGradeArray[c].equals("MS")) {
                stipend[c] = 15 * myStudentArray[c];

            } else {
                stipend[c] = 20 * myStudentArray[c];
            }
        }
        for (double d : stipend) {
            sum += d;
        }

        for (int d = 0; d < 2; d++) {
            JOptionPane.showMessageDialog(null, myNameArray[d] + "-"
                    + stipend[d] + "\n");

        }
    }
}

Dani AI

Generated

A few focused fixes will get the output exactly like you want and avoid the multiple popups.

  • Use the array length instead of hard-coded 2 so loops scale: for (int i = 0; i < myNameArray.length; i++).
  • Don’t use int sum = 0; for money — use double total = 0.0; (your code currently truncates cents).
  • Build one multiline message and call JOptionPane.showMessageDialog once. JLabel (what JOptionPane uses) doesn’t reliably honour "\n", so build simple HTML or use a JTextArea.
  • Normalize grades before comparing (e.g. grade = grade.trim().toUpperCase() or use equalsIgnoreCase) to avoid mismatches.

Example (drop into your program where myNameArray and stipend are ready):

import java.text.NumberFormat;
import javax.swing.JOptionPane;

NumberFormat cf = NumberFormat.getCurrencyInstance();
StringBuilder sb = new StringBuilder("<html>");
double total = 0.0;

for (int i = 0; i < myNameArray.length; i++) {
    sb.append(myNameArray[i])
      .append(" - ")
      .append(cf.format(stipend[i]))
      .append("<br>");
    total += stipend[i];
}
sb.append("<br>Total: ").append(cf.format(total)).append("</html>");
JOptionPane.showMessageDialog(null, sb.toString());

If you want columns to line up, wrap the content in <html><pre>...</pre></html> and use String.format("%-20s %8s", name, cf.format(value)) inside the loop. Also handle possible null from cancelled input and trim user input before using it.

This follows ’s hint about building the string first, fixes the int/double bug, and keeps the UI to a single nicely formatted dialog for all tutors.

In FOR loops, it's alwas good to use yourArray.length instead of a number in FOR's condition.
As for your output, try generating line 56 into a String before you showMessageDialog it.

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.