• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Integer.parseInt(String)

 
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)
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • 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 VI Editor Java
  • 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 VI Editor Java
  • 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.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic