1

I implemented two Hashtable as shown below.

HashMap<Integer,String> streetno=new HashMap<Integer,String>();
   streetno.put(1, "Sachin Tendulkar");
   streetno.put(2, "Dravid");
   streetno.put(3,"Sehwag");
   streetno.put(4,"Laxman");
   streetno.put(5,"Kohli");


HashMap<String,Integer> streetname=new HashMap<String,Integer>();
   streetname.put("Sachin Tendulkar",1);
   streetname.put("Dravid",2);
   streetname.put("Sehwag",3);
   streetname.put("Laxman",4);
   streetname.put("Kohli",5);
   Iterator itr=streetno.keySet().iterator();

Now I'm asking user for input. If he enters the integer I want to retrieve particular value from the 1st hash table and if user enter the string I want to read the particular integer from 2nd hashtable.

My question is "How can I read the input from the user??" Because I don't know whether user enters the integer or string?? And I also wanted to know Can I retrieve particular value using Iterator depending on the key??

6 Answers 6

1

How can I read the input from the user??

Something like

String line = scanner.nextLine();

Because I don't know whether user enters the integer or string??

Try to convert it to an integer

try {
  int num = Integer.parseInt(line);
  // lookup by number

} catch(NumberFormatException ignored) {
  // lookup by string

}

Can I retrieve particular value using Iterator depending on the key??

An Iterator is for iterating, a Map is for looking up by key.

Sign up to request clarification or add additional context in comments.

Comments

1

First, you'll need to read the standard input. Handle it first as a String, to simplify.

   Scanner scanner=new Scanner(System.in);
   String userInput=scanner.nextLine();

Then, try to convert the String to an Integer. If the user typed a number, you'll get the key a the first Map, otherwise, you can asume it's a String key for the second one:

   String sName;
   Integer iNumber;
   try {
       iNumber=Integer.parseInt(userInput);
       //user intruduced a number, so get the streename
       sName=streetno.get(iNumber);

   } catch (NumberFormatException e) {
       //user introduced a not-numeric String, so get the streetnumber
       sName=userInput;
       iNumber=streetname.get(sName);
   }

You don't need to iterate over the keyset, as the Map.get() method will return you the corresponding value.

finally, don't forget to release the scanner (better in a finally block):

scanner.close();

Comments

0

You can read the input as mentioned in other answers. I just tried the main logic in this way. If combining two maps can be done, you can try this way.

            String input = "Laxman";            
            HashMap<String, String> street = new HashMap<String, String>();
            street.put("1", "Sachin Tendulkar");
            street.put("2", "Dravid");
            street.put("3", "Sehwag");
            street.put("4", "Laxman");         

            for (Map.Entry<String, String> entry : street.entrySet()) { 
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                if (input.equals(entry.getKey())) {
                    System.out.println(entry.getValue());   
                    break;
                } else if (input.equals(entry.getValue())) {
                    System.out.println(entry.getKey());   
                    break;
                }               
            }                

Comments

0

I believe you'll need to use Integer.parseInt to figure out whether the input is and integer or not.

Unfortunately, there isn't really a "tryParse" or something like that in java, so you'll have to just catch the exception. You could use a regex to check whether it is numeric or not, but this might be more hassle than it is worth.

As far as reading in from the console, I would use:

Scanner console = new Scanner( System.in );

Comments

0

Read the input as a string; if it can be parsed as an Integer, use streetno, otherwise use streetname.

Comments

0

You can use the Scanner class to retrieve user input. It has several useful methods like nextInt() or nextLine() which can be used to your advantage to determine if a user has entered an int or a string.

You can also make the user "say" what they are entering beforehand - int or string. But this is not very user friendly.

To get a value from a HashMap use the get(key) method, when providing the key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.