View Single Post
  #1 (permalink)  
Old 08-12-2007, 05:42 PM
elimmom elimmom is offline
Member
 
Join Date: Aug 2007
Posts: 2
elimmom is on a distinguished road
Cannot solve the coding problem of my assignment
Assignment: add a social security number as an array of integers with size 9; have their social security number be determined by taking their age times 121314151 and adding their ID number. You must figure out how to get this number into the array. Cut any digits greater than the 9 you need from the left of the answer. the social security number should be refigures whenever the age is changed and should start as 000-00-0000.

I write as the following:
public Student()
{
firstName = "Joan";
lastName = "Doe";
age = 1;
GPA = 0.0;
numberOfStudents++;
ID = numberOfStudents * 12 + 143;
socialSecurityNumber = setupSSN(age);
}

public int [] setupSSN (int currentAge)
{
int count;
int tempStore;
int [] newNumber = new int [9];
int SSNInt = currentAge * 121314151 + ID;
String SSN = Integer.toString(SSNInt);

for (count=0; count<newNumber.length; count++)
{
tempStore = Integer.parseInt(SSN.substring(count.count + 1));
newNumber[count] = tempStore;
}
return(newNumber);

I put "public, int, new int, for, return" in bold style.

When I compile, it compiles fine. But when I try to run it, it says:

java.lang.NumberFormatException: For input string: "-"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:474)
at.java.lang.Integer.parseInt(integer.java:497)
at Student.setupSSN(Student.java:61)
at Student.setSSN(Student.java:69)
at unit11asmnt.main(unit11asmnt.java:46)

Someone told me to turn the 121314151 into a shorter number such as 12131. I did that, but then it came up with:

java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.substring(String.java:1765)
at Student.setupSSN(Student.java:61)
at Student.><int>(Student.java:32)
at unit11asmnt.main(unit11asmnt.java:23)

Could anyone help?
Reply With Quote
Sponsored Links