Results 1 to 10 of 10
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default 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
    Last edited by Zebra; 04-29-2008 at 01:57 PM.
    I am a Java n00b.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    And what is your question?

    What have you done so far?
    freedom exists in the world of ideas

  3. #3
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    I changed the question around to be understanded. (friendly bump)
    I am a Java n00b.

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Talking

    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.

    Java 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!");
    }
    		
       }
    
    }
    Last edited by DonCash; 04-29-2008 at 02:51 PM.
    Did this post help you? Please me! :cool:

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Or we can cut the ugliness of checking for each state with an if :)
    Java 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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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 me! :cool:

  7. #7
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    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.

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Looking at the list in daniels code, 200-51-300 isnt in there. Thats why its not matching.
    Did this post help you? Please me! :cool:

  9. #9
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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 @ [www.littletutorials.com]
    Language is froth on the surface of thought

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

Similar Threads

  1. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  2. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:11 AM
  3. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  4. Problem with Sequential File and Arrays
    By rhivka in forum New To Java
    Replies: 8
    Last Post: 07-30-2007, 09:03 PM
  5. Arrays in Java
    By hiranya in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •