Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Error Inputs
    primarykey
    data
    text
    <p>Here I'm trying to retrieve values and keys from the Hash map depending upon the user input.</p> <p>Here I've asked the user to input values from the key board. And later depending upon the users input I retrieve either Value or Key from the Hash Map.</p> <p>As of now it's working fine. But I want to handle the error conditions.</p> <p>E.g:</p> <ol> <li>What if user give other than M/N or Q in the 1st System.out.println() statement.</li> <li>What if user invalid Main Number and so on.</li> </ol> <p>Like this I want to handle all possible error conditions after asking the user for input in the System.out.println() statement.</p> <p>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cricketpur;</p> <pre><code> import java.lang.*; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // TODO code application logic here HashMap&lt;String, String&gt; streetno = new HashMap&lt;String, String&gt;(); streetno.put("1", "Sachin Tendulkar"); streetno.put("2", "Dravid"); streetno.put("3", "Sehwag"); streetno.put("4", "Laxman"); streetno.put("5", "Kohli"); Scanner s = new Scanner(System.in); String a; String inp; for (;;) { System.out.println("Enter M/N and Q to quit:"); a = s.nextLine(); if ((a == null ? "M" == null : a.equals("M")) || a.equals("m")) { System.out.println("Enter the Main number:"); inp = s.nextLine(); } else if ((a == null ? "Q" == null : a.equals("Q")) || a.equals("q")) { break; } else { System.out.println("Enter the Street name:"); inp = s.nextLine(); } for (Map.Entry&lt;String, String&gt; entry : streetno.entrySet()) { if (inp.equals(entry.getKey())) { System.out.println(entry.getValue()); break; } else if (inp.equals(entry.getValue())) { System.out.println(entry.getKey()); break; } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload