| | |
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,757
Reputation:
Solved Threads: 491
•
•
•
•
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 |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework homework html if_statement image inheritance integer j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel julia keytool keyword linux list macintosh method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql sqlite swing terminal testautomation threads transforms tree ui unicode validation web windows






