Instagram
youtube
Facebook
Twitter

Java Map program using Java HackerRank Solutions

Problem

You are given a phone book that consists of people's names and their phone number. After that you will be given some person's name as query. For each query, print the phone number of that person.

Input Format

The first line will have an integer  denoting the number of entries in the phone book. Each entry consists of two lines: a name and the corresponding phone number.

After these, there will be some queries. Each query will contain a person's name. Read the queries until end-of-file.

Output Format

For each case, print "Not found" if the person has no entry in the phone book. Otherwise, print the person's name and phone number. See sample output for the exact format.

To make the problem easier, we provided a portion of the code in the editor. You can either complete that code or write completely on your own.

Sample Input

3
uncle sam
99912222
tom
11122222
harry
12299933
uncle sam
uncle tom
harry

Sample Output

uncle sam=99912222
Not found
harry=12299933

Solution

import java.util.*;
import java.io.*;

class Solution{
	public static void main(String []argh)
	{
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		in.nextLine();
        Map<String, Integer> phoneBook = new HashMap<>();
		for(int i=0;i<n;i++)
		{
			String name=in.nextLine();
			int phone=in.nextInt();
			in.nextLine();
            phoneBook.put(name, phone);
		}
		while(in.hasNext())
		{
			String s=in.nextLine();
            if (phoneBook.containsKey(s)) {
                System.out.println(s + "=" + phoneBook.get(s));
            } else {
                System.out.println("Not found");
            }
		}
	}
}

Steps involved in this solution:

1. The code begins by importing the required Java libraries using the import statements. It imports classes from java.util and java.io packages.

2. The Solution class is defined, which is the main class of the program.

3. Inside the Solution class, the main method is defined. This method is the entry point of the program and where the code execution starts.

4. Within the main method, a Scanner object named in is initialized to read input from the console. A HashMap named phoneBook is created to store phone book entries. This map associates names (keys) with phone numbers (values).

5. The code reads an integer n from the input using in.nextInt(). This integer represents the number of entries in the phone book. in.nextLine() is used to consume the newline character.

6. A for loop is used to iterate n times to read and store phone book entries. Inside the loop, the code reads a name as a String using in.nextLine() and a phone number as an integer using in.nextInt(). in.nextLine() is again used to consume the newline character. Each name and phone number pair is stored in the phoneBook HashMap using the put method.

7. The code enters a while loop, which continues as long as there is more input to be processed (using in.hasNext()).

8. Inside the loop, the code reads a query name (a person's name) as a String using in.nextLine().

9. The code checks if the query name exists in the phoneBook HashMap using phoneBook.containsKey(s). If the name exists, it prints the name and the associated phone number using System.out.println(s + "=" + phoneBook.get(s)). If the name is not found in the phone book, it prints "Not found."

10. After processing all the queries, the code closes the Scanner (in) to release resources and ensure proper program termination.