Results 1 to 8 of 8
  1. #1
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Unhappy Need help on Exception

    Output i must get







    When i type in the Letters i get "Please enter positive integer values only" instead of "Please enter in numbers, not letters"

    i just can't slove it :mad:


    PHP Code:
    import javax.swing.JOptionPane;
    import java.util.InputMismatchException;
    
    public class Chapter8Q2
    {
       public static void main ( String[] args ) {
    	
    	int age ;
    	String message1 = "Please enter your age in years" + "\n";
    	String message3 = "Please enter positive integer values only"+ "\n";
    	String message4 = "Please enter in numbers, not letter"+ "\n";
    	String temp ,outputMessage ;
    
    try{
    
    	temp = JOptionPane.showInputDialog(message1);
    	age = Integer.parseInt(temp);
    	
    	if( age < 0)
    		throw new IllegalArgumentException();
    	outputMessage = "Your age is "+ age + " years"+ "\n";
    	JOptionPane.showMessageDialog(null, outputMessage);
    	
    }//end try
    
    
    catch( InputMismatchException e)	//non number
    {
    	JOptionPane.showMessageDialog(null, message4);
    }//end catch
    
    
    catch( IllegalArgumentException e)	//negative number
    {
    	JOptionPane.showMessageDialog(null, message3);
    
    }//end catch
    
    
    System.exit(0);
    
    }//main
    }//class
    I LOVE JAVA!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I don't have Java IDE on this machine now. Can you just change two catch statements and see it give any exception there. I'm just seeking on it.

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Your input is related to Integer and its method(s), namely parseInt(). And you want to catch the NumberFormatException because this is thrown if the String cannot be parsed into an int.

    Java Code:
    [COLOR=#000000][COLOR=#0000bb]import javax[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]swing[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]JOptionPane[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#007700]
    
    [/COLOR][COLOR=#0000bb]public [/COLOR][COLOR=#007700]class [/COLOR][COLOR=#0000bb]Chapter8Q2 
    [/COLOR][COLOR=#007700]{ 
       [/COLOR][COLOR=#0000bb]public [/COLOR][COLOR=#007700]static [/COLOR][COLOR=#0000bb]void main [/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000bb]String[/COLOR][COLOR=#007700][] [/COLOR][COLOR=#0000bb]args [/COLOR][COLOR=#007700]) { 
    [B]    Integer age = new Integer(0); // initialization
        [/B][/COLOR][B][COLOR=#0000bb]int year[/COLOR][/B][COLOR=#007700][B]; [/B]
        [/COLOR][COLOR=#0000bb]String message1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"Please enter your age in years" [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#dd0000]"\n"[/COLOR][COLOR=#007700]; 
        [/COLOR][COLOR=#0000bb]String message3 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"Please enter positive integer values only"[/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#dd0000]"\n"[/COLOR][COLOR=#007700]; 
        [/COLOR][COLOR=#0000bb]String message4 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"Please enter in numbers, not letter"[/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#dd0000]"\n"[/COLOR][COLOR=#007700]; 
        [/COLOR][COLOR=#0000bb]String temp [/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]outputMessage [/COLOR][COLOR=#007700]; 
    
    [/COLOR][COLOR=#0000bb]try[/COLOR][COLOR=#007700]{ 
    
        [/COLOR][COLOR=#0000bb]temp [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]JOptionPane[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]showInputDialog[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]message1[/COLOR][COLOR=#007700]);
     
    [B]    [COLOR=#0000bb]year[/COLOR][/B][/COLOR][B][COLOR=#0000bb] [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]age[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]parseInt[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]temp[/COLOR][/B][COLOR=#007700][B]); [/B]
         
        if( [B][COLOR=#0000bb]year[/COLOR][/B][/COLOR][COLOR=#0000bb] [/COLOR][COLOR=#007700]< [/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700]) 
            [/COLOR][COLOR=#0000bb]throw [/COLOR][COLOR=#007700]new [/COLOR][COLOR=#0000bb]IllegalArgumentException[/COLOR][COLOR=#007700](); 
    
        [/COLOR][COLOR=#0000bb]outputMessage [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"Your age is "[/COLOR][COLOR=#007700]+ [B][COLOR=#0000bb]year[/COLOR][/B][/COLOR][COLOR=#0000bb] [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#dd0000]" years"[/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#dd0000]"\n"[/COLOR][COLOR=#007700]; 
        [/COLOR][COLOR=#0000bb]JOptionPane[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]showMessageDialog[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]null[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]outputMessage[/COLOR][COLOR=#007700]); 
         
    }[/COLOR][COLOR=#ff8000]//end try 
    
    
    [/COLOR][/COLOR][B]catch( NumberFormatException e)  // can't parse the String into an int[/B]
    {
            JOptionPane.showMessageDialog(null, message4);
    }
    [COLOR=#000000][COLOR=#ff8000] 
    
    [/COLOR][COLOR=#0000bb]catch[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000bb]IllegalArgumentException e[/COLOR][COLOR=#007700])    [/COLOR][COLOR=#ff8000]//negative number 
    [/COLOR][COLOR=#007700]{ 
        [/COLOR][COLOR=#0000bb]JOptionPane[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]showMessageDialog[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]null[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]message3[/COLOR][COLOR=#007700]); 
    
    }[/COLOR][COLOR=#ff8000]//end catch 
    
    
    [/COLOR][COLOR=#0000bb]System[/COLOR][COLOR=#007700].exit([/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700]); 
    
    }[/COLOR][COLOR=#ff8000]//main 
    [/COLOR][COLOR=#007700]}[/COLOR][COLOR=#ff8000]//class  [/COLOR][/COLOR]
    Hope this helps.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    wow! it's work thx again! Captain Morgan
    Last edited by Deon; 01-26-2008 at 09:28 AM.
    I LOVE JAVA!

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Looking for throwables

    Hello Deon

    If you do not know what exceptions or errors to catch then use this:
    Java Code:
    try{
        int a = 1, b = 0;
        int c = a / b;
    } catch (Throwable e) {
        System.out.println(e.getClass().getName());
    }
    This will tell you what exception was caught. ;)

    I'm going to install my JDK now, so please, mind the bugs.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    thx alot tim!
    I LOVE JAVA!

  7. #7
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default hmmm?

    I have question for this program. When I type the codes and run it, the cancel button is not working. It gives the one of the string message from the program and close. why is that?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What message you found? Can you send it here to see.

    If the message box is not working, what means by,

    It gives the one of the string message from the program and close

Similar Threads

  1. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 03:24 PM
  2. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 08:01 PM
  3. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  4. JSP Exception Hanling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:57 AM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 AM

Posting Permissions

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