Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 04-25-2008, 03:59 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
Arrays Problem (Advanced Java...Need Help)
I am suppose to make an array with a social secruity numbers for each state (fake ones). When the user inputs their SSN then it then returns what state they are from.

For example: if Jim enters
132-11-0987, then the program would respond, "Hello Jim, you we born in
New York State."

Here are the numbers for each state:

001-003 NH 400-407 KY 530 NV
004-007 ME 408-415 TN 531-539 WA
008-009 VT 416-424 AL 540-544 OR
010-034 MA 425-428 MS 545-573 CA
035-039 RI 429-432 AR 574 AK
040-049 CT 433-439 LA 575-576 HI
050-134 NY 440-448 OK 577-579 DC
135-158 NJ 449-467 TX 580 VI Virgin Islands
159-211 PA 468-477 MN 581-584 PR Puerto Rico
212-220 MD 478-485 IA 585 NM
221-222 DE 486-500 MO 586 PI Pacific Islands*
223-231 VA 501-502 ND 587-588 MS
232-236 WV 503-504 SD 589-595 FL
237-246 NC 505-508 NE 596-599 PR Puerto Rico
247-251 SC 509-515 KS 600-601 AZ
252-260 GA 516-517 MT 602-626 CA
261-267 FL 518-519 ID 627-645 TX
268-302 OH 520 WY 646-647 UT
303-317 IN 521-524 CO 648-649 NM
318-361 IL 525 NM *Guam, American Samoa,
362-386 MI 526-527 AZ Philippine Islands,
387-399 WI 528-529 UT Northern Mariana Islands
__________________
I am a Java n00b.

Last edited by Zebra : 04-29-2008 at 03:57 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-26-2008, 04:44 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
And what is your question?

What have you done so far?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-29-2008, 03:57 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
I changed the question around to be understanded. (friendly bump)
__________________
I am a Java n00b.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-29-2008, 04:44 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 240
DonCash will become famous soon enoughDonCash will become famous soon enough
Hey Zebra.

Here is a code example for you. This does exactly what you want.. You will need to finish it off though with the rest of the States. Just carry on with the 'else if' statements.

Code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SSN { public static void main(String[] args) { String sSN = null; String name = null; String state = null; System.out.println("What is your name?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { name = br.readLine(); } catch (IOException ioe) { System.out.println("Error reading name"); System.out.println(" "); System.exit(0); } System.out.println("What is your Social Security Number?"); BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in)); try { sSN = br.readLine(); } catch (IOException ioe) { System.out.println("Error reading Social Security Number!"); System.out.println(" "); System.exit(0); } if(sSN.contains("001-003")){ state = "NH"; System.out.println("Hello " + name + ". You were born in " + state); } else if(sSN.contains("400-407")){ state = "KY"; System.out.println("Hello " + name + ". You were born in " + state); } else if(sSN.contains("004-007")){ state = "ME"; System.out.println("Hello " + name + ". You were born in " + state); } else if(sSN.contains("408-415")){ state = "TN"; System.out.println("Hello " + name + ". You were born in " + state); } else if(sSN.contains("531-539")){ state = "WA"; System.out.println("Hello " + name + ". You were born in " + state); } //etc else { System.out.println("Your Social Security Number: " + sSN + " Does not match any State!"); } } }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-29-2008 at 04:51 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-29-2008, 07:01 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Or we can cut the ugliness of checking for each state with an if
Code:
import java.io.*; import java.util.*; public class SSN { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter name: "); String name = br.readLine(); String ssn = null; String ssnTk1 = null; String ssnTk2 = null; while (true) { System.out.print("Enter SSN: "); ssn = br.readLine(); StringTokenizer st = new StringTokenizer(ssn, "-"); int tkNum = st.countTokens(); if (tkNum < 2) { System.out.println("Invalid SSN: " + ssn + " Try again!"); } else { ssnTk1 = st.nextToken(); ssnTk2 = ssnTk1 + "-" + st.nextToken(); break; } } String state = ssnToState.get(ssnTk2); if (state == null) { state = ssnToState.get(ssnTk1); } if (state != null) { System.out.println("Hello " + name + ". You were born in " + state); } else { System.out.println("Your Social Security Number: " + ssn + " Does not match any State!"); } } public static Map<String, String> ssnToState = new HashMap<String, String>(); static { addState("001-003", "NH"); addState("004-007", "ME"); addState("008-009", "VT"); addState("010-034", "MA"); addState("035-039", "RI"); addState("040-049", "CT"); addState("050-134", "NY"); addState("135-158", "NJ"); addState("159-211", "PA"); addState("212-220", "MD"); addState("221-222", "DE"); addState("223-231", "VA"); addState("232-236", "WV"); addState("237-246", "NC"); addState("247-251", "SC"); addState("252-260", "GA"); addState("261-267", "FL"); addState("268-302", "OH"); addState("303-317", "IN"); addState("318-361", "IL"); addState("362-386", "MI"); addState("387-399", "WI"); addState("400-407", "KY"); addState("408-415", "TN"); addState("416-424", "AL"); addState("425-428", "MS"); addState("29-432", "AR"); addState("433-439", "LA"); addState("440-448", "OK"); addState("449-467", "TX"); addState("468-477", "MN"); addState("478-485", "IA"); addState("486-500", "MO"); addState("501-502", "ND"); addState("503-504", "SD"); addState("505-508", "NE"); addState("509-515", "KS"); addState("516-517", "MT"); addState("518-519", "ID"); addState("520", "WY"); addState("521-524", "CO"); addState("525", "NM"); addState("526-527", "AZ"); addState("528-529", "UT"); addState("530", "NV"); addState("531-539", "WA"); addState("540-544", "OR"); addState("545-573", "CA"); addState("574", "AK"); addState("575-576", "HI"); addState("577-579", "DC"); addState("580", "VI, Virgin Islands"); addState("581-584", "PR Puerto Rico"); addState("585", "NM"); addState("586", "PI Pacific Islands*"); addState("587-588", "MS"); addState("589-595", "FL"); addState("596-599", "PR Puerto Rico"); addState("600-601", "AZ"); addState("602-626", "CA"); addState("627-645", "TX"); addState("646-647", "UT"); addState("648-649", "NM"); } private static void addState(String ssn, String state) { ssnToState.put(ssn, state); } }
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-29-2008, 07:23 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 240
DonCash will become famous soon enoughDonCash will become famous soon enough
Yeah nice work danielstoner.

That is indeed another way to do it, but Zerba is new to Java and I would of thought the 'if then' statements are much easier to understand!
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-01-2008, 04:20 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
I am using that code that daniel gave me and I can't get it to say i am from a state yet. Here is my output.

__________________
I am a Java n00b.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-01-2008, 05:19 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 240
DonCash will become famous soon enoughDonCash will become famous soon enough
Looking at the list in daniels code, 200-51-300 isnt in there. Thats why its not matching.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-02-2008, 02:01 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Things don't happen by magic in code You didn't provide a 200-anything entry in your original post. Find the mappings from SSN to state, fill the initialization static block and then you can find everything. Good luck
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-02-2008, 03:26 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
You could use the power of dark magic.

Just import java.DarkMagic

And everything will work.lol it was to good to pass
__________________
My IP address is 127.0.0.1
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
new to arrays jimJohnson New To Java 1 04-08-2008 04:45 PM
Help needed with java arrays code d24706 New To Java 2 03-07-2008 03:11 AM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
Problem with Sequential File and Arrays rhivka New To Java 8 07-30-2007 11:03 PM
Arrays in Java hiranya New To Java 3 07-30-2007 11:10 AM


All times are GMT +3. The time now is 05:50 AM.


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