java very basic input problem

Reply

Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

java very basic input problem

 
0
  #1
Nov 15th, 2008
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 ..

  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...
  1. e.printStackTrace();

please help me .. i am kind of lost in this
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,562
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 50
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: java very basic input problem

 
0
  #2
Nov 15th, 2008
  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
  1. intage=Integer.parseInt(age);
you can handle it accordingly.
Last edited by jasimp; Nov 15th, 2008 at 2:00 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: java very basic input problem

 
0
  #3
Nov 15th, 2008
Originally Posted by jasimp View Post
  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
  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 ...
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,757
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: java very basic input problem

 
0
  #4
Nov 15th, 2008
Originally Posted by localp View Post
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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 19
Reputation: tonief is an unknown quantity at this point 
Solved Threads: 1
tonief tonief is offline Offline
Newbie Poster

Re: java very basic input problem

 
-1
  #5
Nov 16th, 2008
You can try like this
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,562
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 50
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: java very basic input problem

 
0
  #6
Nov 16th, 2008
Originally Posted by tonief View Post
You can try like this
  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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: java very basic input problem

 
0
  #7
Nov 17th, 2008
Originally Posted by localp View Post
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
  1. e.printStackTrace();
by
  1. System.out.println(age + " is not a valid Integer.");
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC