Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2007, 05:42 PM
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-12-2007, 10:29 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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.
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)); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 02:58 AM
Member
 
Join Date: Jul 2007
Posts: 23
marco is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-13-2007, 12:33 PM
Member
 
Join Date: Aug 2007
Posts: 2
elimmom is on a distinguished road
Thanks to both of you
I am so happy. My tutor did not know how the problem arises. Now I know where I get stucked. Thanks again.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
assignment problem help needed tiggz1980 New To Java 2 02-07-2008 12:14 AM
Help mi solve my error Deon New To Java 3 01-11-2008 06:26 AM
Help On Coding problem mandrake446 New To Java 3 12-08-2007 08:01 AM
Error in my coding one198 New To Java 2 10-13-2007 06:07 AM
Problem in my coding one198 New To Java 9 08-09-2007 11:07 AM


All times are GMT +3. The time now is 08:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org