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

looping back to JOptionPane  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone explain how to have an application restart if the user inputs an incorrect integer in a JOptionPane question? I know how
to do it with the Scanner class but nothing I do seems to work and can't find any direction online.

This is the beginning of my code:

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would your code look like if you were using a Scanner?

Or, better yet, draw up what you think the code should do using pencil and paper in really general terms. Like:
1) Get input from user
2) Determine if the input was good
3) If the input wasn't good, tell the user, and repeat from step 1.

Then, once you have these broad steps, add more details, like, how do you 'repeat from step 1?' What would that look like in more detail? Then, once you have it in as much detail as you can stomach, turn it into code.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should wrap the parsing code inside a try-catch block to avoid the application halting due to a NumberFormatException in the event that the user enters an alpha character...
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:What would your code look like if you were using a Scanner?

Or, better yet, draw up what you think the code should do using pencil and paper in really general terms. Like:
1) Get input from user
2) Determine if the input was good
3) If the input wasn't good, tell the user, and repeat from step 1.

Then, once you have these broad steps, add more details, like, how do you 'repeat from step 1?' What would that look like in more detail? Then, once you have it in as much detail as you can stomach, turn it into code.


I don't know. That's why I'm asking.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably use the following pseudo-code:

1. Declare variable as int inside main()
2. Declare a try-catch block catching NumberFormatException
3. Inside try block declare a while loop and set condition to true
4. Inside the loop prompt user for input, read and parse input to int
5. After prompt inside loop check value with if statement... if valid break out loop and move on... if invalid inform user && continue loop
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume this is complete crap?

 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- JOptionPane is very interesting API, there are lots of fun, have to read Oracle tutorial for working code example

- then is possible quite everything, including usage of keys from numpad ...

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate all the help and know you guys are trying to help me. I'm new to all of this so a lot of what you write sends me off into a bunch of different directions
where I have no idea what I'm doing even more and the confusion grows. Unfortunately, my instructor is basically invisible and we're on our own to
learn this stuff with a less than stellar text.

Thanks for all the tips and advice.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I *think* I'm making progress but not sure. I've gotten down to my if statements but when I try to calculate the regular rate and overtime it says I have to
declare a local variable. I've tried moving the if{ statement regarding overtime to various places but it continues to give the same error. The error is at the very bottom.
Any advice?

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly the formatting in your code is the cause to your many problems, it makes it hard to read and follow... Visually browsing the code I can spot an error on line 32 where you close the main method leaving the rest of statements dangling within the class... The next thing you should know is that if you've came from a C/C++ background if statements can accept ints(zero or non-zero) but not so in Java... if statements in Java can only accept boolean values (true || false) or expressions that evaluate to (true || false), I assume you meant to use == within the if condition instead of =... finally you are trying to use variables before you declare them, there is no declaration for overtime and hourDiff on line 73.

To get you on the right track your code should follow suite this example:

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rico. You're always very helpful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!