• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

java.util.InputMismatchException

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


here the output is java.util.InputMismatchException

what I have understood is if useDelimiter(",") were not used here then as default delimiter is space and jvm doesn't find any space in given scanner input and as a result of which output is InputMismatchException
If I'm wrong please correct me.
can you please highlight the reason of this exception in my code??
also explain when and why this exception comes if possible with examples

thanks in advance
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correctly setting the delimiter to a comma. The problem is in line 8 where you try to use the nextInt() method. "Sue" is not an int.
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Joel

If I change my code to


then also I'm getting same InputMismatchException.
what i know is nextxxx() get next token and then move to next token.
so 1st token Sue isn't matched then next token 5 is matched thus output should be 5 from my perspective but it doesn't. can't understand why???
please explain it


thanks
 
Marshal
Posts: 80649
475
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, not at all.
Read the documentation, which you should always do whenever you use an unfamiliar piece of code. It assume that the next token is an int. You can get rid of tokens with a while loop and the hasNextInt method.
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ritchi. I understood now.
If next token doesn't match Integer regular expression or out of range then InputMismatchException occurs
and here next token is "sue" which doesn't match Integer regular expression so this exception occured

And if I modify the above code to

then output is 5 since next token matches Integer regular Expression.
 
Campbell Ritchie
Marshal
Posts: 80649
475
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that will work (), but you can use a while loop as I mentioned earlier to discard any tokens before the 5. Try that.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roopam Samal wrote:If next token doesn't match Integer regular expression or out of range then InputMismatchException occurs
and here next token is "sue" which doesn't match Integer regular expression so this exception occured...


Is there any particular reason you're using Scanner here?
String[] tokens = "5,sue,true,3".split(",");
would work just as well, and then:
1. You have all the tokens in your string, rather than having to work through sequentially to get each one.
2. All those tokens are Strings, and now you can worry about converting them.

Personally, I don't particularly like Scanner - mainly because I don't think it achieves what it set out to do (make getting different types, especially numbers, from Strings and files simpler) - but I know that Campbell and I disagree on this point.

However, that said, don't let me stop you from "discovering" Scanner. That's what programming's all about.

Winston
 
Campbell Ritchie
Marshal
Posts: 80649
475
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that Scanner can find an empty String unexpectedly. You will find details here. The books I have read, and the Java® Tutorials, do not cover that particular pitfall at all well.
Have you made any progress with discarding tokens?
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchi
I have understood how to discard tokens.


If any other better way is available then please tell me.


thanks
 
Campbell Ritchie
Marshal
Posts: 80649
475
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Far better way to do it, which Rob Spoor taught me:-
 
reply
    Bookmark Topic Watch Topic
  • New Topic