This question already has an answer here:
- How to do an Integer.parseInt() for a decimal number? 9 answers
System.out.println("Enter a number: ");
String in1 = input.nextLine();
Integer input1 = Integer.valueOf(in1);
Float input2 = Float.parseFloat(in1);
Double input3 = Double.valueOf(in1).doubleValue();
System.out.println();
System.out.println("Enter another number: ");
String in2 = input.nextLine();
Integer input21 = Integer.valueOf(in2);
Float input22 = Float.parseFloat(in2);
Double input23 = Double.valueOf(in2).doubleValue();
FloatN fco = new FloatN();
System.out.println();
System.out.println("The sum of both of your numbers is: " + fco.add(input2, input22));
done = true;
I'm well aware that this program is completely impractical, I only wrote it to practice parsing, generics, and interfaces. I tried Integer, which worked fine, but upon trying the Float and Double.add() functions, I get 3 errors:
at java.lang.NumberFormatException.forInputString
at java.lang.Integer.parseInt
at java.lang.Integer.valueOf
I removed the Integer parsers, and the program worked fine. I'm confused as to why I get the errors only when I enter Decimal values and would like someone to help point out what exactly is causing the exception so I can avoid any errors like this in the future, and since removing the Integer parser removes any functionality from the IntegerN class.
Also, if anyone needs the FloatN class for whatever reason:
public static class FloatN implements Summization<Float>{
public FloatN(){}
public Float add(Float a, Float b)
{
return a + b;
}
}
Summization is a generic interface with an add() method.
Thanks in advance.