1

I have a problem but I don't know how to solve it: I need to get a Long number from a String (I get this string in a SAXparser class I wrote to get some fields from an XML file).

Here you are the problematic code:

if (bprice) {
String price = new String(ch, start, length);
System.out.println("Product price: " + price);
bprice = false;
if (price.equalsIgnoreCase("0"))
product.price = null;
else product.price = Long.parseLong(price);
}

(product is an instance from an object where I store all the product information. I got the error in the last line)

I debugged it and I got this:

Exception occurred in target VM: For input string: "1.04" java.lang.NumberFormatException: For input string: "1.04" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Long.parseLong(Long.java:419) at java.lang.Long.parseLong(Long.java:468) at parsers.LectorXML.characters(LectorXML.java:325) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.characters(AbstractSAXParser.java:538) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:464) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205) at parsers.LectorXML.leer(LectorXML.java:71) at parsers.LeerXML.main(LeerXML.java:20) <

5
  • What's your question? It's quite obvious that 1.04 is not a valid long number. Commented Nov 13, 2012 at 21:44
  • You should not use long or floating point numbers to store prices, unless the prices are expressed in cents (104 instead of 1.04). The most precise type for storing monetary data is BigDecimal Commented Nov 13, 2012 at 21:46
  • @dasblinkenlight what's about Double? Commented Nov 14, 2012 at 8:27
  • 1
    @Alberto Double will accumulate errors when you do math with it because its representation of decimal fractions is not precise. Commented Nov 14, 2012 at 11:43
  • Thanks for your reply @dasblinkenlight. I changed my code this morning and I am using BigDecimal instead Double for the prices. I experienced it is slower than working with Doubles Commented Nov 14, 2012 at 16:18

1 Answer 1

2

1.04 is not a valid long. It is in double format.

You need to use Double.parseDouble(String); instead.

Example:

Double.parseDouble(precio1);
Sign up to request clarification or add additional context in comments.

3 Comments

Oh my god!!! After 6 hours coding I didn't see that!! Ohhhhhh damn it! haha. I'm soooooooooooooo sorry for my stupid question... :(. Thank you very much @Nambari
I tried to accept it but I still had to wait 11 minutes when I tried to (still 4 minutes from now to be able to accept it). I am going to have dinner and I will accept it after that, ok? Thank you very much and I am sorry again!
@Alberto: That was just regular note because you are new user to SO. No force (or) time lines on accepting answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.