Results 1 to 8 of 8
  1. #1
    Sikrik34 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default How to get the value for key in hashmap

    In my code I read in a file of states and statecapitals then store them into a hashmap. I then ask the user what the capital is for the random state displayed.The problem I am having is getting the value for the random generated state. When I enter the correct capital for the state, it is still being marked incorrect. Here is my code. Please I need coded examples.

    Java Code:
    try {
     
                Scanner scanner = new Scanner(file);
                String[] values;
    
                Map<String, String> map = new HashMap<String, String>();   
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    values = line.split("-");  
                    String state = values[0];  
                    String capital = values[1];    
                    map.put(state, capital);   
                }
                scanner.close();
                Scanner scan = new Scanner(System.in);
                boolean again=true;
                int correct=0;
                int incorrect=0;
                while(again){
                    List<String> keysAsArray = new ArrayList<String>(map.keySet());
                    Random r = new Random();
                    
                    String randomstate = keysAsArray.get(r.nextInt(keysAsArray.size()));
                    System.out.println("What is the capital of " + randomstate);
                    String capital=scan.nextLine();
                    if(capital.equals(map.get(randomstate))){
                        correct++;
                        System.out.println("Correct");    
                            }
                    else{
                        incorrect++;
                        System.out.println("Incorrect");
                    }
                    
                System.out.println("Another State? Enter true/false");
                again = Boolean.parseBoolean(scan.nextLine());

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to get the value for key in hashmap

    Print out what your program 'thinks' is the correct state/capital.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to get the value for key in hashmap

    Have you printed out the entered capital and that returned by the state to see if they look identical?

    Regards,
    Jim
    Last edited by jim829; 09-26-2014 at 07:54 PM. Reason: Once again - slow draw McGraw. The slowest gun in the west.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Sikrik34 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to get the value for key in hashmap

    I am not sure how to print out what it thinks the capital is

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to get the value for key in hashmap

    Just print what the user entered and print the key/value pair for the state.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Sikrik34 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to get the value for key in hashmap

    Java Code:
    for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey().toString();;
            String value = entry.getValue();
            System.out.println("key, " + key + " value " + value );
        }
    I have tried this to print everything in the map, but i cant figure out how to just print the value for the randomstate displayed

  7. #7
    Sikrik34 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to get the value for key in hashmap

    Java Code:
    System.out.println("What is the capital of " + randomstate);
                    String cap = scan.nextLine() ;
                    String capital1 = map.get(randomstate);
    
                    
                    if(cap.equals(map.get(capital1))){
                        correct++;
                        
                        System.out.println("Correct");    
                            }
                    else{
                        incorrect++;
                        System.out.println(capital1);
                        System.out.println("Incorrect");
                    }
                }
    This is the output I am getting

    What is the capital of Wisconsin
    Madison
    Madison
    Incorrect
    What is the capital of Montana

  8. #8
    Sikrik34 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: How to get the value for key in hashmap

    I have figured it out I needed to change this

    Java Code:
                    if(cap.equals(map.get(capital1))){
                        correct++;
    }
    to this
    Java Code:
     if(cap.equals(capital1)){
                        correct++;
    }

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  2. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  3. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 08:08 PM
  4. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 02:29 PM
  5. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM

Tags for this Thread

Posting Permissions

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