Frodgers
Frodgers

Reputation: 95

String to Int with a + sign

I'm parsing stock data and trying to put it into a SQL database. All of the info from the parse is retrieved as a string. I am using the Integer.parseInt() method to try and convert the strings to integers for some of the info. The issue I am having is with the Change data. When it is a positive change the number has a "+" sign in front of it and I am getting an error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "+0.14" //
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) //
    at java.lang.Integer.parseInt(Integer.java:492) //
    at java.lang.Integer.parseInt(Integer.java:527) //
    at getStockData.main(getStockData.java:91)" (the //s are to signify end lines, having issues with formatting)

My Output is:

Ticker ID: MSFT: Change - [+0.14]

int Change = Integer.parseInt(di.getTextContent());

I don't really know how to get around this error at the moment, and haven't found anything similar to this after googling / searching stackoverflow.

Upvotes: 1

Views: 259

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

The issue is 0.14 is not a valid int. Try using Double.parseDouble(String) to parse the double value. Like

double v = Double.parseDouble("+0.14");
System.out.println(v);

Output is

0.14

Upvotes: 9

Related Questions