• Post Reply Bookmark Topic Watch Topic
  • New Topic

using scanner  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have been trying to "effectively" use and loop the try catch block with the scanner class functions but just cant find my way!
What i do is use a DO ........While within which is placed the try catch block.

I want to use the hasNextSomething functions to return boolean values which wil go into the while statement of the DO........While loop and make sure the program is not terminated bu continues on acceptingvalues till i get decent ones.

I dont know anything about the scanner class.Well almost nothing.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner is a very nice class, but you need to be aware of one of its peculiar habits. Let us assume you have a Scanner reading from the standard input streamNow you can go for numbers, like thisand you enter 1 2 3 and think you have entered three numbers.

You haven't, not quite. What you usually do is to write
1 2 3<enter>
where <enter> means pushing the "enter" key. The Scanner actually sees that as 4 entries, 1, 2, 3, and a line with the empty String in. You may find you have problems with that empty String, and the way to deal with that is to write

inScan.nextLine();

That simply reads and discards the empty line. You will soon see whether it is working correctly; if not, try the nextLine call whenever you use the "enter" key.

If instead of writing 1 2 3 you write 1 2 3.4 you will get an InputMismatchException because one of those tokens is not an int. I thought it would be reasonable to catch that exception, but Rob Prime recently pointed out there is an easier solution, using the hasNextXXX methods.
What you do is test whether it has a next int, which means whether the next token is an int, then you can parse the token. Let's enhance the bit of code I wrote a few minutes ago.I think that ought to work, ensuring that the tokens read are actually ints.

Those of us who are awake will have noticed there is a bit of really bad style in my last bit of code; I shall leave it to you to work out what
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never tried that sort of code, but I think it will work
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't think it would work at all well in a do...while loop, though.
 
Varun Goenka
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
Appreciate your help.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome I hope it worked.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who come upon this thread later, you will probably need a call to next() on the Scanner in the while loops, so as to move on to the next token. Otherwise it will try forever to see whether it is an int, and it isn't.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!