Results 1 to 5 of 5
Thread: Help with Catching Exceptions
- 09-11-2010, 12:31 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Help with Catching Exceptions
I am trying to make this program catch if a character is entered instead of a number. I get a run-time error on line 23, the line that I try to read "something" again. HELP?!??!
Thanks!Java Code:import java.util.Scanner; import java.io.*; class TRY { public static void main(String [] args) { Scanner reader = new Scanner(System.in); double something; //this trys to catch if a charcter is inputed try { something = reader.nextDouble(); } catch (Exception e) { System.out.println("Not a number."); } something = reader.nextDouble(); System.out.println(something); } }
- 09-11-2010, 12:46 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Ok, double something (horrible name for a variable) is of type double.
If you entered a char type, why would you expect it after one fail(it is a double still,or as your code expected to be) to succeed another time?
Other tips: TRY (try pascal case for class names) look it up, its java standard.
you said: //this trys to catch if a charcter is inputed
Does it???
Your code:
what does that catch??? It will catch your exception but do you know what that is? look at your error messages (inside catch block write code to give explanation)Java Code:catch (Exception e)
- 09-11-2010, 12:53 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Ok I know my class name isn't good. It should be 'Try'. And I am very new to catching exceptions. The basic goal of this program is to see if the variable inputted is a number. If not, I want it to tell the user that it is not a number, then give the user another chance to input it. Can you explain how I would do that?
- 09-11-2010, 01:07 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
have a look at this link first: Java 2 Platform SE v1.3.1: Class Double
then have a look at what this code outputs to the console
what is going on? do you need try catch>>Java Code:double myNum = 'a'; System.out.println(Double.isNaN(myNum));
- 09-11-2010, 02:06 AM #5
Some comments on your code:
You should print out a message to the user explaining what he should enter.
When printing out the value of a variable, include the variables name:
System.out.println("something=" + something);
The Scanner class is tricky to use. It reads the user input into a buffer and then gives it to your program when the program asks for it with a next method. If you ask for a double and the buffer contains characters, the nextDouble will throw an exception AND the characters are still in the buffer. You need to use one of Scanner's methods to get the bad data out of the buffer before you ask the user to enter some good data. Also the Scanner class has some testing methods names starting with hasNext. Try experimenting with different combinations of these methods to see how they work with different input.
BTW You'll see an exception with your current code when it calls nextDouble the second time.Last edited by Norm; 09-11-2010 at 02:08 AM.
Similar Threads
-
After catching the exception thrown
By scoobyrox in forum New To JavaReplies: 2Last Post: 09-05-2010, 02:29 PM -
Client catching Web Service User Exceptions [Java 6.0.17]
By Shaitan00 in forum Advanced JavaReplies: 4Last Post: 11-22-2009, 01:09 AM -
catching uncaught exceptions using Spring / WebApp
By taille50 in forum Web FrameworksReplies: 1Last Post: 10-18-2009, 01:14 AM -
unreported exception IOException -- Yet I AM catching it?
By Agathorn in forum New To JavaReplies: 2Last Post: 09-18-2009, 11:22 PM -
AWT - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks