943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 738
  • Java RSS
Nov 15th, 2008
0

java very basic input problem

Expand Post »
i want to pass an integer value as an input, for that first i have to get the value as an string and then convert it into an Integer. i wrote the code and it looked like this ..

Java Syntax (Toggle Plain Text)
  1. String age="";
  2. int intage=0;
  3. System.out.print("Please Enter your age : ");
  4.  
  5. try{
  6. age= dataIn.readLine();
  7. intage=Integer.parseInt(age);
  8. }catch( IOException e ){
  9. e.printStackTrace();
  10. }

but my problem here is that when i enter a String like "dfjdkdjre" , as input , i get an error or rather an exeption.. how do i prevent this ... can i display an error message or ??

can some one help me complete my code .. please

and could some one tell me what does this line do...
Java Syntax (Toggle Plain Text)
  1. e.printStackTrace();

please help me .. i am kind of lost in this
Similar Threads
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Nov 15th, 2008
0

Re: java very basic input problem

Java Syntax (Toggle Plain Text)
  1. e.printStackTrace()
is used for "tracing" where your exception occurred. It is useful to the programmer for debugging, but useless to the end user. Try validating your input before you parse it to an integer. If you catch that exception you get from
Java Syntax (Toggle Plain Text)
  1. intage=Integer.parseInt(age);
you can handle it accordingly.
Last edited by jasimp; Nov 15th, 2008 at 2:00 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 15th, 2008
0

Re: java very basic input problem

Click to Expand / Collapse  Quote originally posted by jasimp ...
Java Syntax (Toggle Plain Text)
  1. e.printStackTrace()
is used for "tracing" where your exception occurred. It is useful to the programmer for debugging, but useless to the end user. Try validating your input before you parse it to an integer. If you catch that exception you get from
Java Syntax (Toggle Plain Text)
  1. intage=Integer.parseInt(age);
you can handle it accordingly.
it didnt work, i get an error when i type a string like " hhfdh "; it works for numbers but not for string ...
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Nov 15th, 2008
0

Re: java very basic input problem

Click to Expand / Collapse  Quote originally posted by localp ...
it didnt work, i get an error when i type a string like " hhfdh "; it works for numbers but not for string ...
I don't know what you did, but you perhaps aren't catching a NumberFormatException. IOException won't catch the error you are referring to. parseInt throws a NumberFormatException in the situation you are referring to. If you are only catching IOException, you are going to get an error.
http://java.sun.com/j2se/1.5.0/docs/...va.lang.String)
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 16th, 2008
-1

Re: java very basic input problem

You can try like this
Java Syntax (Toggle Plain Text)
  1. Buffered reader tast=new BufferedReader( new InputStreamreader(System.in));
  2. String age="";
  3. age=tast.readLine();
  4. intage=Integer.parseInt(age);
Last edited by tonief; Nov 16th, 2008 at 8:10 am.
Reputation Points: 8
Solved Threads: 1
Newbie Poster
tonief is offline Offline
19 posts
since Sep 2008
Nov 16th, 2008
0

Re: java very basic input problem

Click to Expand / Collapse  Quote originally posted by tonief ...
You can try like this
Java Syntax (Toggle Plain Text)
  1. Buffered reader tast=new BufferedReader( new InputStreamreader(System.in));
  2. String age="";
  3. age=tast.readLine();
  4. intage=Integer.parseInt(age);
They already have that. If you actually read the thread they were having trouble with an exception.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 17th, 2008
0

Re: java very basic input problem

Click to Expand / Collapse  Quote originally posted by localp ...
but my problem here is that when i enter a String like "dfjdkdjre" , as input , i get an error or rather an exeption.. how do i prevent this ... can i display an error message or ??
by making sure to use only valid input..
or, just by replacing
Java Syntax (Toggle Plain Text)
  1. e.printStackTrace();
by
Java Syntax (Toggle Plain Text)
  1. System.out.println(age + " is not a valid Integer.");
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: [HOMEWORK] Need help with java project
Next Thread in Java Forum Timeline: grid in jsp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC