简体   繁体   中英

Handling Error Inputs

Here I'm trying to retrieve values and keys from the Hash map depending upon the user input.

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.

As of now it's working fine. But I want to handle the error conditions.

Eg:

  1. What if user give other than M/N or Q in the 1st System.out.println() statement.
  2. What if user invalid Main Number and so on.

Like this I want to handle all possible error conditions after asking the user for input in the System.out.println() statement.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cricketpur;

  import java.lang.*;
  import java.util.*;
  import java.io.*;
  public class Main {


public static void main(String[] args) {
    // TODO code application logic here

    HashMap<String, String> streetno = new HashMap<String, String>();
    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<String, String> 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;
            }

        }
    }
}
}

You have a hashmap. You don't have to compare keys. Simply see if streetno.get(inp) is null or not. If it's not null, you can display the value, and if it is null, then you can display an error message.

Also, "M"==null is false, always. null is a special value meaning "no value". Comparing non-null values to null will not get you what you want. You can simplify your checks as:

if("M".equals(a) || "m".equals(a)) { ... }

Your current code will still barf on null inputs, because of the a.equals("m") bit which is run. (I do, however, believe that Scanner will return an empty string, not null if the user just hits Enter.)

Check to see if their input is valid, and if it isn't then ask them to input it again

System.out.println("Enter M/N and Q to quit:");    
while(true)
{
    a = s.nextLine();
    if(/*a is valid*/)
    {
        break;
    }
    else
    {
        System.out.println("Insert invalid input message here.  Enter M/N and Q to quit:");    
        a = s.nextLine();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM