NumberFormatException: For input string: "-"
Multiplying
121314151 by larger factors causes it to overflow the maximum positive value on an int and the
parseInt method cannot deal with the minus sign that results. So you can move up to the next higher primitive value container:
long.
Here are some ideas about how you can explore some of these issues.
public class Test {
public static void main(String[] args) {
System.out.printf("Integer.MAX_VALUE = %d%n Long.MAX_VALUE = %d%n",
Integer.MAX_VALUE, Long.MAX_VALUE);
// testRange(121314151);
int age = 20;
int numberOfStudents = 1;
int ID = numberOfStudents * 12 + 143;
int[] socialSecurityNumber = setupSSN(age, ID);
print(socialSecurityNumber, "socialSecurityNumber");
}
public static int[] setupSSN(int currentAge, int ID)
{
System.out.println("currentAge = " + currentAge + " ID = " + ID);
int n;
int[] newNumber = new int[9];
// Use a long to hold the high values.
long SSNInt = (long)currentAge * (long)121314151 + (long)ID;
System.out.println("SSNInt = " + SSNInt);
String SSN = Long.toString(SSNInt);
System.out.println("SSN = " + SSN);
int j = SSN.length()-1;
int k = newNumber.length-1;
while(k >= 0)
{
if(j < SSN.length()-1)
n = Integer.parseInt(SSN.substring(j, j+1));
else
n = Integer.parseInt(SSN.substring(j));
//System.out.printf("j = %d n = %d%n", j, n);
newNumber[k] = n;
j--;
k--;
}
return newNumber;
}
private static void print(int[] n, String s) {
System.out.print(s = "[");
for(int j = 0; j < n.length; j++) {
System.out.print(n[j]);
if(j < n.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}
private static void testRange(int n) {
// At what age/count does a positive int value overflow?
int count = 0;
int p;
int limit = Integer.MAX_VALUE;
System.out.println("limit = " + limit);
do {
p = count++ * n;
System.out.println("p = " + p);
} while(p > -1 && p < limit);
System.out.println("p exceeded the limit of positive int " +
"value at " + (count-1));
}
}