Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Failing to apply multiple lines in JOptionPane  RSS feed

 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening,

I have a question that will likely seem petty. I am new to Java, and having trouble inserting multiple lines into a single JOptionPane. The code is below, and as you will see, I had each JOptionPane popping up individually, but that is it. I have tried the \n, but it didn't work. I have tried creating a string, and that didn't work. I have scoured several pages of Google attempting to figure this out, and will continue to do so.






JFrame frame1 = new JFrame();

String to_print=
"Car Rental Rate for 10 days : $" +total+ "\n"+
        "Discount Percent : " +discount3+ "%" "\n" +
        "Estimated Total : $"  +netRate+;
        "Car Rental Rate"
        JOptionPane.PLAIN_MESSAGE);

     JOptionPane.showMessageDialog(frame1, to_print);
 


                                             JOptionPane.showMessageDialog(frame1,
        "Car Rental Rate for 10 days : $" +total+ "",
        "Car Rental Rate",
        JOptionPane.PLAIN_MESSAGE);





                                              JOptionPane.showMessageDialog(frame1,
        "Discount Percent : " +discount3+ "%",
        "Car Rental Rate",
        JOptionPane.PLAIN_MESSAGE);
 
                      JOptionPane.showMessageDialog(frame1,
        "Estimated Total : $"  +netRate+ "",
        "Car Rental Rate",
        JOptionPane.PLAIN_MESSAGE);

}
}
 
Fred Kleinschmidt
Bartender
Posts: 507
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, when yopu post code, please highlight the code and select the "Code" tag; this makes it much easier to read the code. Also, do not include extraneous whitespace Here is your code, cleaned up a bit:


When the object you pass to JOptionPane.showMessageDialog() is a String, JOptionPane places that string in a JLabel to display it. A JLabel displays its text on a singler line (ignoring '\n' characters). If you want multiple lines, you have to format the text string using HTML.

Also, you are repeating the message with multiple JOptionPane calls. - remove the duplicates.


Later you can learn how to include everything - the messages plus the input fields for the user to fill out - using a single JOptionPane call.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thank you for your input. I clicked submit before I was finished preparing the message, and then I couldn't figure out how to edit. So my apologies. Thank you for your help.
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the \n, but it didn't work. 

Please post the code that didn't work and explain what happened when you compiled and executed it.
The \n works for me.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I have tried the \n, but it didn't work. 

Please post the code that didn't work and explain what happened when you compiled and executed it.
The \n works for me.


I will use the HTML course of action as previously directed. I do however have a question that you may be able to answer. As you will see in the code below, I am trying to declare, but not initialize days, rentals, and referrals since they will be user input. This is causing total and discount to display the error "The local variables have not been initialized." Any suggestions?


 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
There is nothing wrong with that question, but it is an interesting question which told me something I didn't know before. A few cows being thrown round, and the thread will move to our GUIs forum.

[edit]In view of new question I shall copy this discussion to the GUIs forum rather than moving it. Don't double‑space all your code, because it doesn't make it any easier to read. I have removed the empty lines.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Wayne wrote:. . . display the error "The local variables have not been initialized." Any suggestions? . . .
Unlike fields, local variables do not have a default value, so they must be definitely assigned to some value or other before they can be used. You will have to initialise those variables.

But why are they local variables? Why haven't you got a Rental class which you are instantiating with duration of rental? Or a calculateCost method which takes duration of rental as a parameter?
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The local variables have not been initialized." Any suggestions?

Initialize the variables by assigning them values.
 
Liutauras Vilda
Marshal
Posts: 3820
200
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Wayne wrote:having trouble inserting multiple lines into a single JOptionPane.
Welcome to the Ranch again.

Hints hardly ever help you here. Revise the method I wrote some time ago:
That should give you an idea.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
"The local variables have not been initialized." Any suggestions?

Initialize the variables by assigning them values.


Thank you both for your answers, and I am sure that you're correct. However, when I initialize them with, for instance, int days=0; when I run the program it gives me zero in the JOptionPane. The scenario is, upon running the program, the user inputs days, cars, and referrals, and then the discount will be calculated based on the inputs.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I just figured it out. I need to do individual JOptionPane calls with each input correct?
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using option pane for keyboard input? That is a very old‑fashioned programming style. Consider this for keyboard input instead.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using option pane for keyboard input? That is a very old‑fashioned programming style. Consider this for keyboard input instead.


It's for class sir. I don't write the instructions; I only execute them.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking I need something such as input.nextInt(); behind the declared integer.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to use option pane for input, you get a String and the easiest way to get an int from that String is probably with a method of this class.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the replies. For future reference in case someone browses onto this post; What I was looking for is this.

JOptionPane.showInputDialog(null, "your message: ", "dialog box header",JOptionPane.PLAIN_MESSAGE);
int a = new Integer(input).intValue();
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But I would have usedAvoid new Integer(...) because Integer has the standard sign that you shou‍ld avoid the constructor. It has a valueOf method (actually it has three valueOf methods). If you find a class with a valueOf method, assume the author of the class looks on that method as their favourite way to instantiate the class.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
But I would have usedAvoid new Integer(...) because Integer has the standard sign that you shou‍ld avoid the constructor. It has a valueOf method (actually it has three valueOf methods). If you find a class with a valueOf method, assume the author of the class looks on that method as their favourite way to instantiate the class.


I'm going to look this up now, because I think it may be the root of the issue I am discussing in my other post.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust you understood how I telescoped the call, so the return value from the option pane dialogue method is passed directly to parseInt?
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I trust you understood how I telescoped the call, so the return value from the option pane dialogue method is passed directly to parseInt?


Not particularly, but you pointed me in the right direction. I will read up on it now. Thanks sir
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Wayne wrote:. . . . Thanks sir
That's a pleasure
If you don't understand it soon, please ask and we shall try to explain it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!