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.