wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Throwing an exception with integers and strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throwing an exception with integers and strings" Watch "Throwing an exception with integers and strings" New topic
Author

Throwing an exception with integers and strings

Ray Zeigler
Greenhorn

Joined: Jul 06, 2012
Posts: 18
Hello, Im looking for an example of how to throw an exception for an input string that;

rejects characters in the alphabet such as "abc..."
and only accepts numbers in the form of "1 2 3.."

Im fairly new to java and Im just looking for a solid example of this code.

-Nelson
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

Generally, exceptions should be thrown only for unexpected conditions, those that you can't foresee and control through your code. It would be better if you can avoid to throw an exception by using some sort of validation (an if-else for example).

For educative purposes and to answer your question, a simple way to throw an exception for a "wrong" input String would be (in pseudocode):

read String;
if String contains alphabetical characters, then throw Exception (IllegalArgumentException would be a good candidate in our case);

So you should consider two things: First, create a proper validation method for your String and then throw the appropriate exception if your input contains alphabetical characters. The validation part will be up to you, the part of throwing an exception can be covered here: http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Hope this helps.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11164
    
  16

I agree. But regardless of whether you use exceptions or not, the first step is to define what EXACTLY is and is not allowed. What about symbols? What about commas, decimal points, negative signs? In accounting practices, I believe parentheses are acceptable to indicate a negative value.

And don't forget that different locations in the world use the '.' and the ',' in the exact opposite manner...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 37
I would create a validation method that accepted an input String and returned true/false if it was valid or not. As others have suggested, check for all possible input characters. Is white space allowed (space, tab)? How big can the input string be? If it represents an integer, you can use Integer.parseInt(String s) to see if it is valid. This method will throw a NumberFormatException if it cannot parse the String as an int.


Follow me: @CompGuyProbs
Read me: Freehold Computers Examiner
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing an exception with integers and strings
 
Similar Threads
writing the files for particular time
Broadcasting to addresses
Advise please
way to convert text to vaild string
https or SSL