• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on Stopping Automatic Dialog Closing.
 
Mike Rose
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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(...);
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic