• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check input is integer in Java.  RSS feed

 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I have a question on this. I have to ask how many children's tickets you want to order. When I apply the code below, it just accepts whatever I input, including letters. I imagine it is to do with setting childrensTickets = 0? If I input a letter using the below it accepts - shouldn't it print the error given the input is not >=0?


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geronimo Finch wrote:Okay. I have a question on this. I have to ask how many children's tickets you want to order. When I apply the code below, it just accepts whatever I input, including letters. I imagine it is to do with setting childrensTickets = 0? If I input a letter using the below it accepts - shouldn't it print the error given the input is not >=0?

Nope, because if the Scanner doesn't have a "next Int" (and a letter isn't an Int), you set the value to 0 yourself and then check 'if (childrensTickets >=0)'.

So, is 0 >= 0?

You tell me what happens next...

Winston

BTW: You could help yourself (and us) out a lot by indenting your code properly.
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program accepts the value as being 0 as that is the value I have assigned childrensTickets. I realise that is wrong.

That's where I'm confused. I'm unsure what to set 'childrensTickets' to. I know it shouldn't be '=0' but what else should I set it to? Or am I way off track here? Do I need to set it to something?
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry didn't see your indenting comment before posting.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geronimo Finch wrote:That's where I'm confused. I'm unsure what to set 'childrensTickets' to. I know it shouldn't be '=0' but what else should I set it to?

How about -1? That not >= 0 is it?

Or indeed, don't set it at all, and just initialize it to -1.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the difference between the variables validChildrenValue and validValue or are they supposed to be the same variable.

BTW when using a boolean value in a while loop, if statement etc you shouldn't compare it to true or false, it is true or false so you can just use it ie
or
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check whether an input is an integer with a regular eexpression or whether it is a Java® int with the hasNextXXX methodsYou can ensure you get an int similarly but need the bang operator too (as Rob Spoor told me a long time ago).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can ensure you get an int similarly but need the bang operator too (as Rob Spoor told me a long time ago)...

Wouldn't it be nice if Scanner had a peekInt() method? Long overdue.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is the facility in Scanner to wind back the stream and revisit previous tokens, but I have never worked out how to do that. The problem with Scanner is not the class itself but (as you said before) there isn't a decent tutorial anywhere.

Note the trick with the loop doesn't work for files, only for keyboard input.
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, everyone. Thanks for the input so far. I think I have the integer check thing worked out.



What I want to know now is if I can input a line after this piece of code:



similar to something like '(if adultTickets ==0) print below




else If (adultTickets >0) to skip the above and go to the this segment:





So basically contingent on the adultTickets input I want to perform an if/else with these two different segments but is that possible and where do the brackets go, do they need to cover each entire piece of code?
*Just to clarify, adults and student/OAPS can't both go. Only one of each. Adults is one group and student/OAPS is another group.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!