| | |
java very basic input problem
![]() |
•
•
Join Date: Apr 2008
Posts: 102
Reputation:
Solved Threads: 2
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 ..
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...
please help me .. i am kind of lost in this
Java Syntax (Toggle Plain Text)
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...
Java Syntax (Toggle Plain Text)
e.printStackTrace();
please help me .. i am kind of lost in this
Local P ...
Java Syntax (Toggle Plain Text)
e.printStackTrace()
Java Syntax (Toggle Plain Text)
intage=Integer.parseInt(age);
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
•
•
Join Date: Apr 2008
Posts: 102
Reputation:
Solved Threads: 2
•
•
•
•
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 fromJava Syntax (Toggle Plain Text)
e.printStackTrace()you can handle it accordingly.Java Syntax (Toggle Plain Text)
intage=Integer.parseInt(age);
Local P ...
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
it didnt work, i get an error when i type a string like " hhfdh "; it works for numbers but not for string ...
http://java.sun.com/j2se/1.5.0/docs/...va.lang.String)
•
•
Join Date: Sep 2008
Posts: 19
Reputation:
Solved Threads: 1
You can try like this
Java Syntax (Toggle Plain Text)
Buffered reader tast=new BufferedReader( new InputStreamreader(System.in)); String age=""; age=tast.readLine(); intage=Integer.parseInt(age);
Last edited by tonief; Nov 16th, 2008 at 8:10 am.
•
•
•
•
You can try like this
Java Syntax (Toggle Plain Text)
Buffered reader tast=new BufferedReader( new InputStreamreader(System.in)); String age=""; age=tast.readLine(); intage=Integer.parseInt(age);
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
•
•
•
•
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 ??
or, just by replacing
Java Syntax (Toggle Plain Text)
e.printStackTrace();
Java Syntax (Toggle Plain Text)
System.out.println(age + " is not a valid Integer.");
![]() |
Similar Threads
- very basic (Java)
- Weird Binary to Integer Problem (Java)
- Alright this loop problem is bugging me (Java)
- Beyond C++ console program.. (C++)
- I/O : simple but not yet solved (Java)
- substring problem (Java)
- noobie asking for assistance with file parsing... (Perl)
- Thinking OOP!: From BASIC to JAVA (Java)
Other Threads in the Java Forum
- Previous Thread: [HOMEWORK] Need help with java project
- Next Thread: grid in jsp
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac map method methods mobile netbeans notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






