my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes How do I make sure the user enters a correct int and double using a JOptionPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I make sure the user enters a correct int and double using a JOptionPane?" Watch "How do I make sure the user enters a correct int and double using a JOptionPane?" New topic
Author

How do I make sure the user enters a correct int and double using a JOptionPane?

Mike Rose
Greenhorn

Joined: Mar 09, 2013
Posts: 2
I have a program where I need to add a new item to an array or modify an existing item and the user has to enter in the information. Most of the information is strings, so It doesnt matter what the user enters but two values need to be specific or I get an error message. I need an Integer and a Double. Here is the snippets of code that I am using right now for the logic, and it works perfectly if the user enters what they should enter...



and



Now there are more boxes above them as it is a big nested if statements going through single boxes at a time. I know I could maybe change everything to a textfield and ask for all the information at once, but the instructor for out class has provided us with examples that look like this instead and I am trying to stay true to the concept of the assignment.

So my question is, how would I check what the user enters here and make sure they are of the right value?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
Read the section from the Swing tutorial on Stopping Automatic Dialog Closing.
Mike Rose
Greenhorn

Joined: Mar 09, 2013
Posts: 2
Rob Camick wrote:Read the section from the Swing tutorial on Stopping Automatic Dialog Closing.


I get from that that I can set a specific action needed to close the window. But I honestly dont know how to use that to check if what the user entered was an int where required and a double where required. I apologize for my noobness, I am taking my first Java class ever and trying to learn quite a bit about its many functions.

The only section that I think would fit what Im trying to do is...



If i understand this, I would add this to my code and put in the values I was checking, and if they were not true, the window woudnt close. I would also need to make the changes to my frame class where it is set to exit on close now. But Even if I am right with my understanding here, I do not know how to tell it to check for an int value and then a double value.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
I honestly dont know how to use that to check if what the user entered was an int where required and a double where required


See Integer.parseInt(...) and Double.parseDouble(...);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I make sure the user enters a correct int and double using a JOptionPane?