Question

3. Write a simple phonebook program that reads in a series of name-number pairs from the user (that is, name and number on onWriting in Java ,using Arraylist and collection please.

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 PhoneBook {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, String> map = new HashMap<>();
        String name, number, choice;
        do {
            System.out.print("Enter name: ");
            name = in.next();
            System.out.print("Enter number: ");
            number = in.next();
            map.put(name, number);
            System.out.print("Do you want to try again(y or n): ");
            choice = in.next();
        } while (!choice.equalsIgnoreCase("n"));
        System.out.print("Enter name to search for: ");
        name = in.next();
        if (map.containsKey(name)) {
            System.out.println(map.get(name));
        } else {
            System.out.println(name + " is not in the phone book");
        }
    }
}

Enter name: ronaldo Enter number: 7 Do you want to try again(y or n): Y Enter name: messi Enter number: 10 Do you want to try

Add a comment
Know the answer?
Add Answer to:
Writing in Java ,using Arraylist and collection please. 3. Write a simple phonebook program that reads...
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
  • In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program...

    In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program that reads a number of strings from the user then displays them in reverse order. 3. Modify your code in (2) so that MyStack become a generic class (i.e. MyStack).

  • Write a Java program that first reads a positive integer from the user - let's call...

    Write a Java program that first reads a positive integer from the user - let's call it howMany. Then the program reads howMany pairs of integers - let's call them n1 and n2. For each pair, the program determines if the first component in the pair is a multiple of the second component, in other words, if n1 is a multiple of n2. For example, if numberOfPair is 5, your program will read 5 pairs of integers, and for each...

  • Please solve in java Write a program that serves as a simple calculator. The program should...

    Please solve in java Write a program that serves as a simple calculator. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. You'll need to read the operator as a String and compare it using Strings, as you did in 4.1. Turn in your source...

  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • PLEASE UPLOAD or Write a simple PSEUDO CODE AND JAVA CODE for this program WITH COMMENTS...

    PLEASE UPLOAD or Write a simple PSEUDO CODE AND JAVA CODE for this program WITH COMMENTS IN BOTH TELLING WHAT EACH PART DOES. (I NEED BOTH CODES NOT JUST ONE OR THE OTHER) Problem Statement A golf club has a small tournament consisting of five golfers every weekend. The club president has asked you to design a program that does the following: Reads player’s names and their scores from the keyboard for each of the five golfers and simulates writing...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

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