Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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] + "
");

        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

Basically, for each column, you need some kind of list (either an array or preferably an List). You also need to maintain information on each columns max-width (I typically put this in another List).

You need to loop each line, breaking to down into it's column elements and place each element in the corresponding column List. You should also be calculating the width requirements for each column as you go.

Once you have this all structured out, you need to then reconstruct the out put, using something like a StringBuilder to rebuild each line of the output but so that each column has the required additional spacing to allow the columns to flow.

Updated with example

Now, normally, I would store each row as an individual object, as it makes it easier to maintain a consistent idea of the total number of rows, but, using you code as a jumping off point...

import java.text.NumberFormat;

public class TestTextColumnFormatting {

    public static void main(String[] args) {
        String[] myNameArray = new String[]{
            "Mr. Samete",
            "Ms. Torbura",
            "Mr. Perem",
            "Mr. Rothaw",
            "Miss. Endemos",
            "Mr. Chomoshon",
            "Mrs. Aldir",};
        double[] stipends = new double[]{
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        double tally = 0;

        int columnWidths[] = new int[2];
        for (int index = 0; index < myNameArray.length; index++) {
            columnWidths[0] = Math.max(columnWidths[0], myNameArray[index].length());
            columnWidths[1] = Math.max(columnWidths[1], nf.format(stipends[index]).length());
            tally += stipends[index];
        }

        columnWidths[1] = Math.max(columnWidths[1], nf.format(tally).length());

        StringBuilder sb = new StringBuilder(128);
        for (int index = 0; index < myNameArray.length; index++) {

            String name = myNameArray[index];
            String stipend = nf.format(stipends[index]);

            sb.append(name).append(fill(name, columnWidths[0], ' ')).append(" ");
            sb.append(fill(stipend, columnWidths[1], ' ')).append(stipend);
            sb.append("
");

        }


        sb.append(fill("", columnWidths[0], ' ')).append(" ");
        sb.append(fill("", columnWidths[1], '=')).append("
");
        sb.append(fill("", columnWidths[0], ' ')).append(" ");
        sb.append(fill(nf.format(tally), columnWidths[1], ' ')).append(nf.format(tally));
        sb.append("
");

        System.out.println(sb.toString());

    }

    public static String fill(String sValue, int iMinLength, char with) {

        StringBuilder filled = new StringBuilder(iMinLength);

        while (filled.length() < iMinLength - sValue.length()) {

            filled.append(with);

        }

        return filled.toString();

    }
}

This will then output something like...

Mr. Samete      $500.00
Ms. Torbura     $200.00
Mr. Perem       $300.00
Mr. Rothaw    $1,000.00
Miss. Endemos   $250.00
Mr. Chomoshon   $125.00
Mrs. Aldir      $600.00
              =========
              $2,975.00

Updated

I'm such an idiot, I just realised that you're using a JOptionPane :P

The simplest solution would be to construct a HTML table and let Swing render it for you...

enter image description here

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

public class TestTextColumnFormatting {

    public static void main(String[] args) {
        String[] myNameArray = new String[]{
            "Mr. Samete",
            "Ms. Torbura",
            "Mr. Perem",
            "Mr. Rothaw",
            "Miss. Endemos",
            "Mr. Chomoshon",
            "Mrs. Aldir",};
        double[] stipends = new double[]{
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        double tally = 0;

        StringBuilder sb = new StringBuilder(128);
        sb.append("<html><table border='0'>");
        for (int index = 0; index < myNameArray.length; index++) {

            String name = myNameArray[index];
            String stipend = nf.format(stipends[index]);

            sb.append("<tr><td align='left'>");
            sb.append(name);
            sb.append("</td><td align='right'>");
            sb.append(stipend);
            sb.append("</td></tr>");

            tally += stipends[index];

        }
        sb.append("<tr><td align='center'>");
        sb.append("</td><td align='right'>");
        sb.append(nf.format(tally));
        sb.append("</td></tr>");

        JOptionPane.showMessageDialog(null, sb.toString());

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...