Question

Language: Java Topic: HashMap, Sets, if - else statements Question: How do you check if your...

Language: Java

Topic: HashMap, Sets, if - else statements

Question: How do you check if your Scanner input matches any of the objects in a HashMap?

For example, if you have

HashMap<String, String> hash = new HashMap<>();

The hash map includes String objects, with the key being names, and value being age number and their address.

if you ask for string name in scanner , how do you see if the hashmap contains this key given by user scanner. And then give the value of this key., if it exist in hashmap.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

package collections;

import java.util.*;

public class HashMapQuestion {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

System.out.println("Enter name : ");

String name=in.nextLine();

int c=0;

HashMap<String,String> map=new HashMap<>();

map.put("Rahul","Age - 26 , Address - Preet Vihar");

map.put("Neha"," Age -22 , Address - Azad Nagar");

map.put("Roy"," Age -24 , Address - Lakshmi Nagar");

for(Map.Entry<String,String> m:map.entrySet()) {

if(m.getKey().equals(name+"")) {

c=1;

System.out.println("The Age and Address of ");

System.out.println(m.getKey()+" is : "+m.getValue());}}

if(c==0) {

System.out.println("Entry does not match! ");}

}

}

Output:

Enter name :

Neha

The Age and Address of

Neha is : Age -22 , Address - Azad Nagar

Add a comment
Know the answer?
Add Answer to:
Language: Java Topic: HashMap, Sets, if - else statements Question: How do you check if your...
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
  • Use Java language You may assume that you always map strings to integers, and therefore you...

    Use Java language You may assume that you always map strings to integers, and therefore you need not worry about using generics. Here are the methods that your map must implement. void put (String key, Integer value); insert this key-value pair into the map under the following rules: If key already exists in the map, change its value to value, overwriting what was there If key is not already there, insert this key-value pair into the map. boolean containsKey(String key);...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

    Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase of program compilation Assignment: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a Java, program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described...

  • In Java JOptionPane how do you validate if a user enters an integer value? How do...

    In Java JOptionPane how do you validate if a user enters an integer value? How do you reprompt the user to enter an integer if they first entered a double or a string value? int length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length: ")); int width = Integer.parseInt(JOptionPane.showInputDialog("Enter the width: ")); int height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height: "));

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Can someone help me with this code, I not really understanding how to do this? import...

    Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...

  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • I've done all questions except question 6. how do I do this? please help. code needs...

    I've done all questions except question 6. how do I do this? please help. code needs to be java 1. You need to be able to open and read the text files that have been provided for the project. Write a Java program that reads the driver.txt file and displays each data value for each line from the file to the screen in the following format: Licence Number: Licence Class: First Name: Last Name: Address: Suburb: Postcode: Demerit Points: Licence...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones...

    This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known. The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted. Create a Java application that uses: decision constructs looping constructs basic operations on an ArrayList of objects...

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