Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Exceptions. Please HELP! (Java)


  • Please log in to reply

#1
coolio2005

coolio2005

    New Member

  • Member
  • Pip
  • 1 posts
Hi, I was just wondering if someone can write me a sample code for exception. Nothing fancy, just a few lines. Lets say ive got my main function and thats doing something. In my main , im asking a user to input their first name (of course, this is string). What i want to do is, if the user has entered in nothing or if they've entered in a number (as a person cannot have numbers in their name), then i want an exception just saying that "You have entered a number as your first name, please try again". Basically, i want my exceptions to be handled in another class completely and i would like them to be triggered when that happens. Could someone help me please? Thanks.
Edit/Delete Message
  • 0

Advertisements


#2
PoRco

PoRco

    Member

  • Member
  • PipPip
  • 14 posts
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 ! " );
}



}
  • 0

#3
PoRco

PoRco

    Member

  • Member
  • PipPip
  • 14 posts
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.
  • 0

#4
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
Research REGEX topic on Java.
  • 0

#5
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
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.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP