• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to force user to enter intenger input between 1 and 9??  RSS feed

 
amro talaat
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

its not working
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amro talaat wrote:
its not working


First, please TellTheDetails... (see link)

Second, when the nextInt() method hits a token that is not an int, it throws an exception. So, there is no need to clear the token at line 9.

Third, also related to point two, you will need to catch the exception if you want to handle the bad input case.

Fourth, the nextInt() method fetches the next int token, so, calling it twice at line 6 will fetch two integers. So, the "if" condition at line 6 doesn't make much sense.

And finally, you didn't store the int result (from any of the next calls) anywhere.

Henry
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amro talaat wrote:its not working

Hi amro, and welcome to JavaRanch.

I hate to say, but we have a page for this type of query: ItDoesntWorkIsUseless. Click the link.

What doesn't work? Does it compile? Does it not do what you expect? If not: how?

We need details to answer your question properly.

However: one thing I will give you for free: What do you think
  c.nextInt() > 9 || c.nextInt() < 1
does?

Hint: What does c.nextInt() do?

And another tip: space out your code a bit more. You don't get any prizes for making it as short as possible, and it makes it much more readable.

HIH

Winston
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And probably "do while" loop would be the one to use here.

Welcome again.
 
amro talaat
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
amro talaat wrote:
its not working


First, please TellTheDetails... (see link)

Second, when the nextInt() method hits a token that is not an int, it throws an exception. So, there is no need to clear the token at line 9.

Third, also related to point two, you will need to catch the exception if you want to handle the bad input case.

Fourth, the nextInt() method fetches the next int token, so, calling it twice at line 6 will fetch two integers. So, the "if" condition at line 6 doesn't make much sense.

And finally, you didn't store the int result (from any of the next calls) anywhere.

Henry


Iam expecting the program to keep telling the user "try again" if he entered a number not in this range. If he entered Acceptable number it will tell him "thanks for entering the number". How can i achieve this?!!!
 
amro talaat
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amro talaat wrote:
Henry Wong wrote:
amro talaat wrote:
its not working


First, please TellTheDetails... (see link)

Second, when the nextInt() method hits a token that is not an int, it throws an exception. So, there is no need to clear the token at line 9.

Third, also related to point two, you will need to catch the exception if you want to handle the bad input case.

Fourth, the nextInt() method fetches the next int token, so, calling it twice at line 6 will fetch two integers. So, the "if" condition at line 6 doesn't make much sense.

And finally, you didn't store the int result (from any of the next calls) anywhere.

Henry


Iam expecting the program to keep telling the user "try again" if he entered a number not in this range. If he entered Acceptable number it will tell him "thanks for entering the number". How can i achieve this?!!!


Also i dont care for exceptions, and i dont want to store his input.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amro talaat wrote:

Also i dont care for exceptions, and i dont want to store his input.


If you don't care for handling exceptions, then line 9 serves no purpose. Just remove it.

And if you don't want to store the input, not even in a temporary local variable, then you will have to find a way to do the condition check by calling the nextInt() method only once... which arguably, while interesting to do, what's wrong with a temporary local variable?

Henry
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amro talaat wrote:Iam expecting the program to keep telling the user "try again" if he entered a number not in this range. If he entered Acceptable number it will tell him "thanks for entering the number".

OK, so what does your program do right now? Anything? We need to know the details.

How can i achieve this?!!!

By writing a program that works ... and I'm afraid nobody here is going to do that for you.

What we will do - when we have sufficient information - is to help YOU to write it yourself by answering specific questions you have about problems you're running into.

Winston
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search my posts for Scanner and utility class you will probably find enough information to achieve what you want.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!