Question

In Java write a program that asks user’s name, records it in memory, prints the number...

In Java write a program that asks user’s name, records it in memory, prints the number of times it saw the name since it was last started and goes back to asking user’s name.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class NameCounts {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map = new HashMap<>();
        String name;
        while (true) {
            System.out.print("Enter a name(empty string to exit): ");
            name = in.nextLine();
            if (name.isEmpty())
                break;
            if (!map.containsKey(name)) {
                map.put(name, 0);
            }
            map.put(name, map.get(name)+1);
            System.out.println("Number of times " + name + " is seen is " + map.get(name));
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
In Java write a program that asks user’s name, records it in memory, prints the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT