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
  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • ***** JAVA ONLY ***** Write a program that asks the user to enter the name of...

    ***** JAVA ONLY ***** Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad to create a simple file that can be used to test the program. ***** JAVA ONLY *****

  • Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the...

    Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the input in capital letters, printCaps(String input, int num) prints in capital letters the specified number of times on a new line: public static void printCaps(String input) public static void printCaps(String input, int num)

  • Write a program that asks for 'name from the user and then asks for a number...

    Write a program that asks for 'name from the user and then asks for a number and stores the two in a dictionary [called the_dicr) as key-value pair. The program then asks if the user wants to enter more data [More data ly/n]?) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list Note: Ignore the case where the name is already...

  • In Java, write a program that asks the user for the name of a file. The...

    In Java, write a program that asks the user for the name of a file. The program should read all the numbers from the given file and display the total and average of all numbers in the following format (three decimal digits): Total: nnnnn.nnn Average: nnnnn.nnn Class name: FileTotalAndAverage

  • Write a Java program which asks the user to enter an integer x. This program prints...

    Write a Java program which asks the user to enter an integer x. This program prints the remainder of x when it is divided by 3. Example Pl ea s e e n t e r an i n t e g e r : 3 remainde r=0 Pl ea s e e n t e r an i n t e g e r : 17 remainde r=2

  • Write a program that asks the user for a negative number, then prints out the product...

    Write a program that asks the user for a negative number, then prints out the product of all the numbers from -1 to that number Enter a negative number: -6 720 (The answer is 720 because-1x-2x-3x-4x-5 x-6 720).

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • 2. Write a java program that asks you to enter your last name, your country of...

    2. Write a java program that asks you to enter your last name, your country of origin, your age, and your expected graduation year. After you enter them, write out each answer on a separate line. You must use Scanner to input the 4 fields, and println to output the 4 fields separately.

  • C++ Write a program that asks for a number and then prints as many lines as...

    C++ Write a program that asks for a number and then prints as many lines as the user inputs. Each line contains as many pairs of characters ("*#") as the number of this line. It should look like the right half of a pyramid. Your version of the program must print the same result as the expected output. To to this lab, you must use two do-while loops. Two exceptions: When the user inputs a number less than 1, then...

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
ADVERTISEMENT