| | |
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,829
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 |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






