Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-26-2008, 06:16 AM
Deon's Avatar
Member
 
Join Date: Jan 2008
Posts: 12
Deon is on a distinguished road
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


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(nulloutputMessage);
    
}
//end try


catch( InputMismatchException e)    //non number
{
    
JOptionPane.showMessageDialog(nullmessage4);
}
//end catch


catch( IllegalArgumentException e)    //negative number
{
    
JOptionPane.showMessageDialog(nullmessage3);

}
//end catch


System.exit(0);

}
//main
}//class 
__________________
I LOVE JAVA!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-26-2008, 06:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,408
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-26-2008, 07:34 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 750
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.

Code:
import javax.swing.JOptionPane; public class Chapter8Q2 { public static void main ( String[] args ) { Integer age = new Integer(0); // initialization int year; 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); year= age.parseInt(temp); if( year< 0) throw new IllegalArgumentException(); outputMessage = "Your age is "+ year+ " years"+ "\n"; JOptionPane.showMessageDialog(null, outputMessage); }//end try catch( NumberFormatException e) // can't parse the String into an int { JOptionPane.showMessageDialog(null, message4); } catch( IllegalArgumentException e) //negative number { JOptionPane.showMessageDialog(null, message3); }//end catch System.exit(0); }//main }//class
Hope this helps.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-26-2008, 09:11 AM
Deon's Avatar
Member
 
Join Date: Jan 2008
Posts: 12
Deon is on a distinguished road
wow! it's work thx again! Captain Morgan
__________________
I LOVE JAVA!

Last edited by Deon : 01-26-2008 at 09:28 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-26-2008, 09:20 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
Looking for throwables
Hello Deon

If you do not know what exceptions or errors to catch then use this:
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-26-2008, 09:27 AM
Deon's Avatar
Member
 
Join Date: Jan 2008
Posts: 12
Deon is on a distinguished road
thx alot tim!
__________________
I LOVE JAVA!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where does the exception go? aytidaalkuhs New To Java 3 04-07-2008 03:24 PM
exception Oktam New To Java 2 03-23-2008 08:01 PM
Trouble with factory method - unhandled exception type Exception desmond5 New To Java 1 03-08-2008 07:41 PM
JSP Exception Hanling Java Tip Java Tips 0 12-24-2007 10:57 AM
Exception Camden New To Java 2 11-27-2007 12:50 AM


All times are GMT +3. The time now is 04:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org