• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for user input to be only integers in Java  RSS feed

 
Arcadin Nguyen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on lottery game (user inputs 6 number, i will generate 8 unique winning numbers and 2 last numbers are supplementary). I need help with user input checking if input numbers are from 1 to 45 and input must be int, when input is not integer it throws an error.

This programming way is procedure way, how can i change it into object oriented way? I know that I must make methods in another java file and then link it back to this main. Can you suggest me how to do it?

I have tried try and catch, if and else (for input check) but i don't know how to check user input when it's in array. Thank you for help.

Here is my code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't look closely at your question or your code, but this leaped out at me:


That says, "If check is simultaneously both less than 0 and greater than 45."

Do you know of any numbers that meet that condition?

And there appear to be a lot more like that.
 
Arcadin Nguyen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:I didn't look closely at your question or your code, but this leaped out at me:


That says, "If check is simultaneously both less than 0 and greater than 45."

Do you know of any numbers that meet that condition?

And there appear to be a lot more like that.


Hi Jeff,
I have made a mistake there. It should be > 0 and < 45. I have edited it.

My program runs well, I only have problem to check user input.
I think this piece of code is totally wrong.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arcadin Nguyen wrote:
I have made a mistake there. It should be > 0 and < 45. I have edited it.

My program runs well, I only have problem to check user input.
I think this piece of code is totally wrong.


You need to stop and think for a moment.

What does (check > 0 && check < 45) mean?

What are some numbers for which that test will be true? What do you want to do with those numbers?

What are some numbers for which that test will be false? What do you want to do with those numbers?
 
Arcadin Nguyen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want user input to be between 1 and 45 if it's not, it will throws an error that it's out of range and user can re-enter numbers again.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arcadin Nguyen wrote:I want user input to be between 1 and 45 if it's not, it will throws an error that it's out of range and user can re-enter numbers again.


That doesn't answer my questions.

What does (check > 0 && check < 45) mean?

What are some numbers for which that test will be true? What do you want to do with those numbers?

What are some numbers for which that test will be false? What do you want to do with those numbers?

If you're going to write code, you have to pay attention to detail, and you have to think precisely and express yourself precisely.
 
Arcadin Nguyen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does (check > 0 && check < 45) mean?

It check if user input into array is from 1 to 45.

What are some numbers for which that test will be true? What do you want to do with those numbers?

If test is true, user can continue in the program and get result from the program.

What are some numbers for which that test will be false? What do you want to do with those numbers?

If test if false, it will throws an error and user must re-enter new array.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arcadin Nguyen wrote:What does (check > 0 && check < 45) mean?

It check if user input into array is from 1 to 45.

What are some numbers for which that test will be true? What do you want to do with those numbers?

If test is true, user can continue in the program and get result from the program.


Okay, so you seem to understand that (check > 0 && check < 45) means that the value of check is between 0 and 45, although you seem confused about the endpoints, as you know that it doesn't include 0, but you seem to think it does include 45.

You also know that if the test evaluates to true (that is, the number is in the range 1..44) that you want the user to be able to continue on.

Now go back and look at your code:


Do you see how the code you wrote disagrees with what you said you understand and what you said you want to happen?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!