• 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:

Throwing exceptions for wrong data types?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, in the case that I want to throw an exception when the wrong type of data type is entered, how can I do this with IllegalArgumentException?



For instance, if someone were to enter a double, string, or boolean input into the scanner instead of an integer, how could I throw an exception for this? I have to do this for an assignment, and they specifically said that I need to use a throw exception for this. Thanks everyone in advance!
 
author
Posts: 23959
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

Sameer Kan wrote:For instance, if someone were to enter a double, string, or boolean input into the scanner instead of an integer, how could I throw an exception for this? I have to do this for an assignment, and they specifically said that I need to use a throw exception for this. Thanks everyone in advance!



The Scanner class nextInt() method will throw an InputMismatchException exception, if the token is not an integer. So, you can catch the InputMismatchException instance and throw an IllegalArgumentException upon catching in the catch block.

Now, having said that, if this is a homework assignment, then I don't think your instructor wants you to use the nextInt() method. Instead, your instructor is probably expecting you to use the next() method instead. This method returns a raw token (string), which you can parse out the integer (or throw the exception).

Henry

 
Saloon Keeper
Posts: 11066
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry has mentioned a way to deal with the problem. I'm not sure I agree with Henry though. Seems like you could use hasNextInt() to do what you want.
 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are ways to ensure that an error message is passed if the wrong data type is entered, allowing you to enter new data, and avoid all exceptions. I think Carey is right, but in case of doubt, ask your teacher for clarification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic