• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.parseInt(String)

 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my own practice code.
But i could not understand why i get this output.Why there is NumberFormatException in line1

output:
Enter age
23
23
Exception in thread "main" java.lang.NumberFormatException: For input string: "2
"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at exc.Test.main(Test.java:28)
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




This all sounds problem inviting. You got readLine() method in BufferedReader which directly returns your String.
check readLine()>
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,readLine() worked very well.
But i was just testing read() method in this program.
String p is printing alright.Then what could be the problem parsing it to int.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


String p is printing alright.Then what could be the problem parsing it to int.



Add SOPs inside the loop and see what each character it prints. char[] array is of size 3 , check out what is n.length() and check for any special characters in the final String ( space may be misleading when you print like that )
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. The space in end is the problem.
I changed the code as suggested by you.
See the outputs.





outputs:C:\IOEX\classes>java exc.Test
Enter age
456
456
4
5
6
Error

C:\IOEX\classes>java exc.Test
Enter age
56
56
5
6

Exception in thread "main" java.lang.NumberFormatException: For input string: "5
"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at exc.Test.main(Test.java:32)

So,using trim() made the code run.



 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oh great . I like the way people work out their own code and find issues just by guiding them.
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,thank you so much.
I am out of touch from core concepts for almost 2 months.
And i have forgotten so much. .
Now i understand why Arulk always says" keep applying what you have learnt in the certification result forum.
I hope ranchers here will help me just like you did.
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic