• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Integer.parseInt(String)

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic