-
Java (
http://www.daniweb.com/forums/forum9.html)
| localp | Nov 15th, 2008 1:26 pm | |
| java very basic input problem 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 ..
String age="";
int intage=0;
System.out.print("Please Enter your age : ");
try{
age= dataIn.readLine();
intage=Integer.parseInt(age);
}catch( IOException e ){
e.printStackTrace();
}
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...
e.printStackTrace();
please help me .. i am kind of lost in this :( |
| jasimp | Nov 15th, 2008 1:57 pm | |
| Re: java very basic input problem 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 intage=Integer.parseInt(age); you can handle it accordingly. |
| localp | Nov 15th, 2008 2:23 pm | |
| Re: java very basic input problem Quote: Originally Posted by jasimp (Post 736761) 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 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 ... |
| VernonDozier | Nov 15th, 2008 4:24 pm | |
| Re: java very basic input problem Quote: Originally Posted by localp (Post 736770) 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) |
| tonief | Nov 16th, 2008 8:07 am | |
| Re: java very basic input problem You can try like this
Buffered reader tast=new BufferedReader( new InputStreamreader(System.in));
String age="";
age=tast.readLine();
intage=Integer.parseInt(age); |
| jasimp | Nov 16th, 2008 8:44 am | |
| Re: java very basic input problem Quote: Originally Posted by tonief (Post 737143) You can try like this
Buffered reader tast=new BufferedReader( new InputStreamreader(System.in));
String age="";
age=tast.readLine();
intage=Integer.parseInt(age); | They already have that. If you actually read the thread they were having trouble with an exception. |
| stultuske | Nov 17th, 2008 4:38 am | |
| Re: java very basic input problem Quote: Originally Posted by localp (Post 736740) 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
e.printStackTrace(); by
System.out.println(age + " is not a valid Integer."); |
| All times are GMT -4. The time now is 7:43 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC