In the code below, I am getting a java.lang.NumberFormatException when entering large numbers ~1000000000. The error occurs on the second-last line -\
int integer = Integer.parseInt(split[0]);
It should work in theory since Integer type supports till 2^32-1 but it doesn’t
Scanner user_input = new Scanner(System.in);
//accept user input and convert into double
String s = user_input.next();
double number = Double.parseDouble(s);
System.out.println(number);
String answer = "";
//Split the entered number into Integer and Decimal parts
String split[] = Double.toString(number).split("\\.");
int integer = Integer.parseInt(split[0]);
int decimal = Integer.parseInt(split[1]);
The problem is probably because of the representation of a large double which will be in scientific notation:
This will mean that the split is failing, try printing out the value of the string you are parsing.
When you call toString on a double, the format used can vary, try using DecimalFormat instead: