Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this program, you will create a program to read input, create a hashmap, and display value. The Hashmap has an Integer key and a String value denoting a student id and name. The program searches whether a key and determines whether the key-value pair is present if present it displays the value or else displays -1

The goal of the program is to practice the basics of collections in java.

The first line of the input is an integer that represents the number of key-value pairs to

be input in the Hashmap. The second line consists of the number of key-value pairs.

The third line consists of the key to be searched for.

Function Description

Complete the function main in the editor below. The function must print the value if

the key is present otherwise display-1

. Read the input from the console 
Create Hashmap with given input values • Check whether the given key is present or not. Print the value if the key is present otherwise display -1

Input Format For Custom Testing

The first line contains an integer, n, denoting the number of elements in
Each line / of the n subsequent lines
(where 0 si<n) contains a key-value pair separated by a space. The last line contains the key to be
searched for.

same case
101 John Doe
102 John Doe

Sample output


The result displayed is -1 because there is no key-value pair with a key value of 105.

Sample case 1

909 Epsilon

Sample Output

The result displayed is "Epsilon" because

there is key-value pair with a key value of


What I have tried:

Learning arrays actually
So how do I take the user input in int and then stire them in array
Updated 6-Jul-21 2:49am

Start by learning the language: Java Tutorials Learning Paths[^].
1) Create an array of integers.
2) Create a loop - a for loop is fine - which iterates as many times as the number of array elements, with an increasing value called "index".
2.1) Inside the loop, read an input from the user into an integer.
2.2) Store the integer you just read into the array using "index" to specify the array element to store it at.

But ... this is your homework, so I'll give you no code!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900