r/javahelp • • Oct 13 '19

Homework How to format a String into rows and columns using a single JOptionPane.showMessageDialog window?

Hello! I am learning Java at university and am having trouble with formatting the output in the way that my lecturer wants. In the question below, I have no problems with parts 1 and 2 but am completely stuck on how I should get my output into 5 columns using JOptionPane.showMessageDialog (here is a link to how he wants it to look).

Question:

  1. Write a program that will calculate and output multiples of a particular value up to a particular limit.

  2. Your program must firstly ask the user to enter a positive integer value followed by an upper limit (in separate input dialog boxes).

  3. It should be noted that the format of your output must correspond exactly to the format of the output of mine (note that the multiples appear in 5 columns).

My lecturer suggested making a new variable 'output', setting it to an empty string, and then putting my output from the for loop into this variable. I have tried using ...

  • "\n"

  • "\n\t\t\t"

  • " " + "\t\t\t" + " " + "\t\t\t" + "\n" //etc, etc

... to no avail. We must use a for loop for this exercise. Could anyone offer me any suggestions? Thank you.

import javax.swing.JOptionPane;
public class Exercise3
{
    public static void main( String[] args )
    {
        String output = "";
        String posInt = JOptionPane.showInputDialog( "Please enter a positive whole number:" );
        int pos = Integer.parseInt( posInt );
    
        String upperLim = JOptionPane.showInputDialog( "Please enter an upper limit:" );
        int uLim = Integer.parseInt( upperLim );
        
        for( int i = pos; i <= uLim; i += pos )
        {
            output += i;
        }

        JOptionPane.showMessageDialog( null, "The multiples of " + pos + " between " + pos + " and " + uLim + " are: " + output );
    }
}
16 Upvotes

7 comments sorted by

2

u/Ilikesmallthings2 Oct 13 '19

Probably not the best solution but you could use a string format in some if conditions to determine which value is divisible by 5 and add a \n to the output format.

output = String.format(output + "%-10s", i); for everything else besides the the 5th one. You would add \n inside the formatting.

1

u/FoxOrchid Oct 13 '19

Oh, is this something to do with html?? I will see if I'm able to implement this solution! Thanks very much for your help!! :D

1

u/Ilikesmallthings2 Oct 13 '19

Not html. I have done any GUI work with java. It just formats your string in that format. Give it a shot and see how it works. Uses the String.format

1

u/[deleted] Oct 13 '19

I'm very unsure but there is a way to use html in those. Maybe have a look into that

1

u/FoxOrchid Oct 13 '19

I saw a similar solution when I was googling how to do this! Thanks so much for your reply, I will try it out :D

1

u/indivisible Oct 14 '19 edited Oct 14 '19

You can approach this in multiple ways. As suggested already, String.format() is one possibility. Another, as you touched on in your question, is tabs \t and newlines \n in a loop (tabs should align your columns as long as your individual values don't get too long that they exceed one single tab's width).

If you think about your output, you only want one newline every 5th value and tabs after all the others.
Here's a simplified, incomplete example loop:

String outputString;
for (int i=0; i<values.length; i++) {
    outputString += values[i];
    if (is a 5th value) {
        outputString += "\n";
    } else {
        outputString += "\t";
    }
}

0

u/AutoModerator Oct 13 '19

It seems that you possibly have a screenshot of code in your post How to format a String into rows and columns using a single JOptionPane.showMessageDialog window? in /r/javahelp.

Screenshots of code instead of actual code text is against the Code posting rules of /r/javahelp as is also outlined in the sidebar - Code posting.

  • Never submit screenshots of code instead of code text!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).

  • Pastebin for programs that consist of a single class only

  • Gist for multi-class programs, or programs that require additional files

  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.

  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot. Talk-to-the-bot is the new talk-to-the-hand. If you instead want the classic talk-to-the-hand, just message the moderators. ;)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.