• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about JOptionPane  RSS feed

 
Jeff Sho
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be very simple, but for my java class I have to assign an int using JOptionPane.showInputDialog. Currently my code looks like this:


int itemPrice;
String userPrompt;

userPrompt = "How much is the item?(in dollars)";

itemPrice = JOptionPane.showInputDialog(null, userPrompt);



but eclipse keeps telling me to change the int to a String, but I need to be able to make an if statement that includes a range of numbers with ">=" (i.e greater than or equal to) which I don't know how to do with a String using "itemPrice.equals".

So basically, can anyone tell me how to either use a string to include a range of numeric input (I can't imagen how this would be possible) or resolve this problem?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Using JOptionPane like that is considered very old-fashioned, but you had better follow your instructions.
That JOptionPane dialogue reads the input as a bit of text, ie a String, and Eclipse is suggesting you alter the reference you are passing the String into.

You will have to convert it to an int, possibly using the method of the java.lang.Integer class which parses Strings into ints.
 
Jeff Sho
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the response! Would you mind explaining the syntax for this? I followed your link, but I couldn't make much sense of it myself.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's there not to get? You take one of the static methods (I'm positive you can find the right one), call it with a String, and retrieve an int.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . one of the static methods . . .
You don't mean the method to parse to an int, do you Rob?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why yes I do. I just didn't want to spoil everything
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I had better parse on that one before I get anybody into trouble.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!