Results 1 to 4 of 4
- 08-12-2007, 04:42 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
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?
- 08-12-2007, 09:29 PM #2
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.
Java Code: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)); } }
- 08-13-2007, 01:58 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 23
- Rep Power
- 0
convert the 123456789 into a string by doing (variables in italics, code in bold)
stringID = Integer.toString (123456789); //turn the int into a string
then copy each digit into the array
for (int i = 0 ; i < stringID.length () ; i++) //run the amount of times as the length of the string
arrayID [i] = stringID.charAt (i); //put that char at the digit of the string into the array
that should do it :)
- 08-13-2007, 11:33 AM #4
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-06-2008, 11:14 PM -
Help mi solve my error
By Deon in forum New To JavaReplies: 3Last Post: 01-11-2008, 05:26 AM -
Help On Coding problem
By mandrake446 in forum New To JavaReplies: 3Last Post: 12-08-2007, 07:01 AM -
Error in my coding
By one198 in forum New To JavaReplies: 2Last Post: 10-13-2007, 05:07 AM -
Problem in my coding
By one198 in forum New To JavaReplies: 9Last Post: 08-09-2007, 10:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks