Edit/Delete Message
Exceptions. Please HELP! (Java)
Started by
coolio2005
, Mar 22 2006 01:04 AM
#1
Posted 22 March 2006 - 01:04 AM
Edit/Delete Message
#2
Posted 29 March 2009 - 04:55 AM
Heres an example, I'll do sort of the opposite of what you asked. I'll ask the user to enter a number and throw an exception if he enters it wrong.
while (noError == false)
{
try{
System.out.println (" Please enter your age : " );
int x = keyboard.nextInt();
if ( (x < 0) || (x > 120))
{
throw new java.lang.Exception();
}
else
{
noError = true;
}
}
catch (InputMismatchException exception)
{
System.err.println (" Wrong input ! Please input numerical integers only !");
}
catch (java.lang.Exception ee)
{
System.err.println ( " Please enter a valid age, that age is impossible ! " );
}
}
while (noError == false)
{
try{
System.out.println (" Please enter your age : " );
int x = keyboard.nextInt();
if ( (x < 0) || (x > 120))
{
throw new java.lang.Exception();
}
else
{
noError = true;
}
}
catch (InputMismatchException exception)
{
System.err.println (" Wrong input ! Please input numerical integers only !");
}
catch (java.lang.Exception ee)
{
System.err.println ( " Please enter a valid age, that age is impossible ! " );
}
}
#3
Posted 29 March 2009 - 04:57 AM
See what I'm doing there? I'm covering both possible errors. Sometimes your code will require you to have the user input a number within a set range.
The other case here is when the user enters a value that does not match the variable you set it to store under as. A "mismatch" . Hence, Input-Mismatch-Exception.
The other case here is when the user enters a value that does not match the variable you set it to store under as. A "mismatch" . Hence, Input-Mismatch-Exception.
#4
Posted 17 November 2009 - 05:53 AM
Research REGEX topic on Java.
#5
Posted 23 November 2009 - 12:35 AM
Regex might be a bit too complicated to be worth learning for this rather simple problem. Although it will certainly prove beneficial if you're so inclined.
Since in this case, you'll be using keyboard.nextLine() instead of nextInt(), no exception will, by default, be thrown if the user inputs a number as their name. The converse, of course, isn't true - you would automatically get an exception if a user entered a letter when a number was expected. But, since numbers can be treated the same as letters (that is, just as ASCII characters), you need to throw your own exception. See this page for info on how to do that.
Obviously, you'll need to write your own input validation checks. I would first check that the user has input anything at all, (i.e. that their input is not equal to ""), then, if that check succeeds, use a for loop to iterate through each character of the input. An easy way to check if a certain character is a letter or not is to call both the ToUpper and ToLower (or maybe it's Upcase and Downcase in Java; I can't remember) methods on the character and check if they are equal. Obviously, if the character is a letter, its uppercase will not be the same as its lower case. The converse is also true- if a character is not a letter, then its uppercase WILL be the same as its lowercase.
If either of these checks fails, that is, if the string entered as the name is equal to "" or if its uppercase is equal to its lowercase, then you need to use a throw statement in order to create an exception. How you handle the exception, and if you choose to do so, is entirely up to you.
Since in this case, you'll be using keyboard.nextLine() instead of nextInt(), no exception will, by default, be thrown if the user inputs a number as their name. The converse, of course, isn't true - you would automatically get an exception if a user entered a letter when a number was expected. But, since numbers can be treated the same as letters (that is, just as ASCII characters), you need to throw your own exception. See this page for info on how to do that.
Obviously, you'll need to write your own input validation checks. I would first check that the user has input anything at all, (i.e. that their input is not equal to ""), then, if that check succeeds, use a for loop to iterate through each character of the input. An easy way to check if a certain character is a letter or not is to call both the ToUpper and ToLower (or maybe it's Upcase and Downcase in Java; I can't remember) methods on the character and check if they are equal. Obviously, if the character is a letter, its uppercase will not be the same as its lower case. The converse is also true- if a character is not a letter, then its uppercase WILL be the same as its lowercase.
If either of these checks fails, that is, if the string entered as the name is equal to "" or if its uppercase is equal to its lowercase, then you need to use a throw statement in order to create an exception. How you handle the exception, and if you choose to do so, is entirely up to you.
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users