사용자 입력에 따라 해시 맵에서 값과 키를 검색하려고합니다.오류 입력 처리
여기 키 보드에서 값을 입력하도록 사용자에게 요청했습니다. 그리고 나중에 사용자 입력에 따라 해시 맵에서 Value 또는 Key를 검색합니다.
현재로서는 문제가 없습니다. 하지만 오류 조건을 처리하고 싶습니다.
는 예컨대 :
- 어떤 경우 사용자는 1에서 System.out.println() 문에 M/N 또는 Q 이외의를 제공합니다.
- 사용자가 주 번호를 잘못 입력하면 어떻게 되나요?
이와 같이 사용자에게 System.out.println() 문의 입력을 요청한 후 가능한 모든 오류 조건을 처리하려고합니다.
/* *이 템플릿을 변경하려면 도구 | 템플릿 *을 선택하고 편집기에서 템플릿을 엽니 다. */ 패키지 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;
}
}
}
}
}
관심이 있으신 분께 : (a == null? "M"== null : a.equals ("M")) || a.equals ("m")'? – NPE
이 조건은 대문자 M인지 작은 지 여부를 확인합니다. 그게 전부입니다. 하나의 조건 중 하나가 만족되면 if 조건 안에 들어갑니다. –