2

I just started learning about HashMaps and I can print them out but I'm having trouble figuring out how to get userInput for both values store them and then print them out.

Or am I looking at this problem in the wrong way?

System.out.println("Let us know about your pets!");     
Map<String, String> pets = new HashMap<>();

String userInput; 
String name;
String type;        
int numberOfPets = 0; 
boolean valid = true; 

try (Scanner scnr = new Scanner(System.in)) {
   do { 
      System.out.println("Enter a name: ");
      name = userInput.put(scnr.nextLine());

      System.out.println("What type of animal is " + (name));
      type = userInput.put(scnr.nextLine());

      System.out.println("Would you like to enter another? (y/n) ");

      numberOfPets++; 

    } while (scnr.next().equalsIgnoreCase("y"));

}

System.out.println("You entered" + number of pets +"pets.");
for (String key : pets.keySet()) {
    System.out.println(key + " is a " + pets.get(key));
}

I want the result to read:

Enter a name: {User enters Eustance}

What type of animal is Eustance:

{User enters dragon}

Would you like to enter another pet?

{Yes} Enter

a name: {User enters Reepicheep}

What type of animal is Reepicheep:

{User enters mouse}

Would you like to enter another pet?

{No}

You entered 2 pets.

Enter one of the pets names (or type END to quit): {User enters Reepicheep} Reepicheep is a mouse.

Nicholas K
  • 14,118
  • 7
  • 25
  • 49

2 Answers2

2

You need to change your code to store the name and type in your HashMap as below, so that it can be later retrieved by simply using pets.get(...)

try (Scanner scnr = new Scanner(System.in)) {
    do {
        System.out.println("Enter a name: ");
        name = scnr.nextLine();

        System.out.println("What type of animal is " + (name));
        type = scnr.nextLine();

        // change made here 
        pets.put(name, type);

        System.out.println("Would you like to enter another? (y/n) ");

        numberOfPets++;

      // here as well coz scanner was skipping the input
    } while (scnr.nextLine().equalsIgnoreCase("y")); 

}
Nicholas K
  • 14,118
  • 7
  • 25
  • 49
  • Thanks so much!! I wasn't sure how to call the keys! By the way do you know why after typing in the first name and type and it asks "Would you like to enter another?"...when type y it skips to what type of animal is it? – JackieTowns Jul 06 '19 at 17:59
  • Why did you un-accept the answer? :( Also, to answer your question that's coz the scanner was skipping the input. More on that can be found [here](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Nicholas K Jul 06 '19 at 18:01
  • Apparently I can only accept one answer because I noticed if I clicked another it disappeared. I'm still learning around here. And thanks for the response. I tried putting userInput = scnr.nextLine(); and just scnr.nextLine(); after the "Would you like to enter another" but that just stops the program. Do you know how to make it not skip? – JackieTowns Jul 06 '19 at 18:07
  • You can accept *only* 1 answer but upvote all/any amount! My answer solves that issue I believe, could you cross check it? – Nicholas K Jul 06 '19 at 18:09
  • If by cross check you mean to test that it works, yes, it does. I upvoted and checked it I'm just still having problems with the scanner line after "Would you enter another?" But I loved your answer! – JackieTowns Jul 06 '19 at 18:15
  • Thank you! I hope you've observed the `while (scnr.nextLine().equalsIgnoreCase("y"));`. It works for me as I am able to enter multiple entries. – Nicholas K Jul 06 '19 at 18:17
  • I got it!! Thanks!! I had while(scnr.next...) instead of while(scnr.nextLine...)!! Thanks so much for all your help!! – JackieTowns Jul 06 '19 at 18:22
1

You have the "read" operation:

pets.get(key)

You just need a "write" operation (during your input loop):

pets.put(key, value)

For complete information, take a look at the JavaDocs for Map, e.g.:

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

There are all sorts of things you can do with a Map!

landru27
  • 1,544
  • 9
  • 17