Reputation:
I am having trouble parsing a string into a primitive.
File file1 = new File("bankData.txt");
Scanner sc = new Scanner(file1);
while (sc.hasNext())
{
char c = sc.next().charAt(0);
if(c == 's')
{
long l = Long.parseLong(sc.next().trim());
String s1 = sc.next().trim();
String s2 = sc.next().trim();
// Line 248 int in = Integer.parseInt(sc.next().trim());
String s3 = sc.next().trim();
double d1 = Double.parseDouble(sc.next().trim());
double d2 = Double.parseDouble(sc.next().trim());
System.out.println(s1);
System.out.println(l);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(in);
System.out.println(d1);
System.out.println(d2);
}
}
The following code is generating
bankData.txtjava.lang.NumberFormatException: For input string: "6131231234"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at bank.Bank.readFile(Bank.java:248)
Here is the bankData.txt file my program is trying to read.
s 1001 John Doe 6131231234 [email protected] 1000.00 0.01 200.00
At first I thought that the whitespace was what was causing the problem but as you can see I've trimed all the returned strings from the scanner trying to eliminate this to no luck. It appears that the the primitive variables long l = Long.parseLong(sc.next().trim());
String s1 = sc.next().trim();
String s2 = sc.next().trim();
were succesfully assigned; I tried confirming this in the eclipse debugger but it just jumped past the breakpoints(Any insights?). Java SE8 NumberFormatExceptionStates
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
Not sure where to go from here.
Thanks in advance, your efforts are appreciated.
Upvotes: 0
Views: 1069
Reputation: 100
The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.
Your value 6131231234 is greater than int max value, 6131231234 is not accommodated into int range, which results into exception.
Please use long instead of int.
Upvotes: 0
Reputation: 117
String str = sc.next().trim();
every time before you want to use str ;just use
System.out.printf("%s%n",str);
to test what it exact is.and you can find where is wrong
Upvotes: 0
Reputation: 349
well, this is the fact: Integer.parseInt() accepts a string param, if the param contains whitespace, the method will throw an exception. But if the param's value is greater than Integer.MAX_VALUE, the exception will be thrown too.
Integer.MAX_VALUE is 2147483647 which is less than your "6131231234", so you should use Long.parseLong()
Upvotes: 0
Reputation: 92
I think the number string is out of range of integer, you should use Long.parseLong() there and it should work.
Upvotes: 0
Reputation: 416
You are trying to convert a number larger than the max size of an Integer in Java.
The largest integer value can be 2,147,483,647. The string you are attempting to convert to an integer would contain a value of 6,131,231,234
Store it into a long value like so
long x = Long.parseLong(sc.next().trim());
Upvotes: 1