Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parsing string to Integer

 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am having problem to convert a string into integer . here is the code.

please tell me what is the problem??
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i get the answer please!
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you seriously becoming impatient after just 14 minutes? PatienceIsAVirtue

Next time you post about a problem, be sure to include any exception you're getting, and any other salient details. Just posting a piece of code and asking "what is the problem" is unlikely to elicit helpful responses, especially if quickly followed by something with an exclamation mark.

So: What is happening if you run this code? Why do you think that is? Note that -despite the class name- there is no casting going on here: Integer.parseInt expects an integer, and nothing else.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay Dittmer.
its My fault!
Exception in thread "main" java.lang.NumberFormatException: For input string: "1600.00"
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 intCast.main(intCast.java:5)

I am getting this exception.
 
Amit Vinod Dali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For tutorial on Converting Strings to Numbers Click here
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UlF mentioned already. i.e,
String value = "1600.00"; this value is not an Integer . instead, use Float or Double
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that 1600.00 can not be parsed to integer?
only integer values can be parsed!
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raza Mohd wrote:It means that 1600.00 can not be parsed to integer?
only integer values can be parsed!

It *can*, but not by passing it to something that expects a string representation of an integer.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah I split the string to get the integer value!
and pass it for the parsing.
Is there any chance to parse it without truncating it to integer?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Integer class? No.
As said before you can use Float or Double classes

Look at this:



You can do the same thing with Float class


 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic