• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

its not working
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 8545
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic