| | |
java very basic input problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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,844
Reputation:
Solved Threads: 503
•
•
•
•
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
Views: 595 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






