Results 1 to 5 of 5
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default 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?!??!

    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);
    	}
    }
    Thanks!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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:
    Java Code:
    catch (Exception e)
    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)

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    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?

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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
    Java Code:
    double myNum = 'a';
    System.out.println(Double.isNaN(myNum));
    what is going on? do you need try catch>>

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    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

  1. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  2. Client catching Web Service User Exceptions [Java 6.0.17]
    By Shaitan00 in forum Advanced Java
    Replies: 4
    Last Post: 11-22-2009, 01:09 AM
  3. catching uncaught exceptions using Spring / WebApp
    By taille50 in forum Web Frameworks
    Replies: 1
    Last Post: 10-18-2009, 01:14 AM
  4. Replies: 2
    Last Post: 09-18-2009, 11:22 PM
  5. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •