Results 1 to 8 of 8
  1. #1
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default JOptionPane.showMessageDialog use

    Hi everyone, I am working on a school project and I am stuck in this line. The code works and I get the result I need but they are all in the same line.

    [code]

    JOptionPane.showMessageDialog(null, "Subtotal: " + String.format("%5.2f", subTotal) + "Tax: " + String.format("%5.2f", orderTax) + "Total: " + String.format("%5.2f", saleTotal));


    [\code]

    I tried using "\n" to make it show in three different lines but I get an error saying illegal character.

    Can someone explain to me how to break this line in three separate lines?

    Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I tried using "\n" to make it show in three different lines but I get an error saying illegal character.

    What was the code that you tried? (it's [/code] to end code markup) And what the compiler message or runtime error?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It said Illegal character because you used the incorrect slash, as you did in the code tag.

    Java Code:
    escape character- /
    Slash- \

  4. #4
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I used the wrong slash in the post in purpose. I tried using the right one and for some reason my post did not show the code.

    See how the code shows when I use the right slash?

    Here:

    Java Code:
    JOptionPane.showMessageDialog(null, "Subtotal: " + String.format("%5.2f", subTotal) + \n + "Tax: " + 
    									String.format("%5.2f", orderTax) + "Total: " + String.format("%5.2f", saleTotal));
    And here:

    Java Code:
    	JOptionPane.showMessageDialog(null, "Subtotal: " + String.format("%5.2f", subTotal) + /n + "Tax: " + 
    									String.format("%5.2f", orderTax) + "Total: " + String.format("%5.2f", saleTotal));

    Anyway, I used the slash in the two possible ways and one way it tells me it is a wrong expression and the other way it tells me illegal charater.

    Does it have to do with the fact that I am using the JOptionPane?

    Thanks for the comments.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You need to concatenate the string "\n".

    Java Code:
    ... + "\n" + ...
    ----------------------

    /n is probably interpreted as "divided by n" (a nonsensical expression) and \n as a variable starting with a backslash (an illegal character)

  6. #6
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Thanks, I thought I had tried like that and it did not work. But I just tried and again and it worked.

    Thanks again pbrock.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome.

    -----------------------

    The following occured to me before, when you posted the lines you had tried...

    It is often a good idea to use multiple expressions when the arguments to a method are complex. This is a bit more readable.

    Java Code:
    String msg = String.format(
            "Subtotal: %5.2f%nTax: %5.2f%nTotal: %5.2f", 
            subTotal, orderTax, saleTotal);
    JOptionPane.showMessageDialog(null, msg);

    Note the use of %n rather than \n in the format string. The difference is somewhat subtle but %n will expand to the correct newline sequence on whatever OS it is being run on whereas \n generates a particular (single) character.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Also, another way to display text in multiple lines is to pass a String[] (a String array) as the message parameter.
    Java Code:
        JOptionPane.showMessageDialog(null, new String[]{"Subtotal: " + String.format("%5.2f", subTotal),
                  "Tax: " + String.format("%5.2f", orderTax),
                  "Total: " + String.format("%5.2f", saleTotal)});
    db

Similar Threads

  1. Replies: 5
    Last Post: 07-29-2009, 07:38 PM
  2. help with JOptionPane.showMessageDialog()
    By gmn1 in forum New To Java
    Replies: 8
    Last Post: 03-30-2009, 10:31 AM
  3. Sick of duplicating JOptionPane.showMessageDialog
    By gdanelian in forum New To Java
    Replies: 2
    Last Post: 11-18-2008, 06:53 PM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 11:45 PM
  5. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •