0

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1.0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:458) at java.lang.Integer.parseInt(Integer.java:499) at newform.fivth.(fivth.java:150) at newform.fivth$3.run(fivth.java:623) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This is the part of the code the error refers to:

while (results4.next())
{
for(int i=1;i<=12;i++)
            {
         x.add(Integer.parseInt(results4.getString(i))+0.5);       

            }  

}
}
catch(SQLException sqlEx){
System.out.println(
"* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
} 
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
user2254366
  • 1
  • 1
  • 3

1 Answers1

2

According to Java, the string "1.0" cannot be represented as an int, but it can be represented a double. The parsing logic for Integer.parseInt expects there not to be a decimal point, even if there is just a zero after the decimal point.

To quote the Javadoc for Integer.parseInt

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.

It doesn't expect a decimal point.

If you must accept a String such as "1.0" and store the resulting int, then I would parse it as Double then call intValue. Instead of the following:

Integer.parseInt(results4.getString(i))

I would use this:

Double.valueOf(results4.getString(i)).intValue()

Of course, you'll have to make the value really is an integer. See this SO question for those details.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357