정보보안공부
Java2_day02 본문
*** NumMatch
-> input = new Scanner(System.in).nextInt();
nextInt() 입력 데이터를 정수형으로처리, 영어 알파벳입력하는 경우 Exception발생
-> if(input < 1 || input >100) { System.out.print("1부터100사이 정수입력하시오:"); }
Exception으로도 표현가능
-> 두가지의 Exception실행 가능
*** NumMatchError1 : 첫번째 Exception실행
-> 첫번째 Exception발생 정수형대신 알파벳 입력하는 경우 예외처리
-> try { input = new Scanner(System.in).nextInt(); 부터
if(input < 1 || input >100 ) { } 까지 try실행
-> catch(InputMismatchException e) { //Exception으로도 가능
System.out.print("숫자만 입력하세요!! >>> ");출력후 continue;를 사용함으로써
밑에 문장 실행안하고 반복문 처음으로 돌아가게 한다.
*** NumMatchError2 : 두번쨰 Exception실행
-> 1부터 100사이 숫자를 입력하시오 부분을 예외 처리한다.
-> if( input < 1 || input > 100) { throw new Exception("1부터 100사이 숫자입력") }
-> catch(Exception e) { System.out.print(e.getMessage);
*** NumMatchError3 : 두가지 Exception한꺼번에 처리
-> 두가지의 Exception 한꺼번에 처리
-> 첫번째 nextInt()에 알파벳 입력하는경우 catch(InputMismatchException e)
{ System.out.print("숫자만 입력하세요 >>> "); } 선언
위에다 java.util.InputMismatchException; 선언
-> 두번째 1미만 100이상 숫자 입력하는 경우 if( input < 1 || input >100 )
{ throw new Exception("1부터 100까지 숫자를 입력하시오 >>> "); }
catch ( Exception e ) { System.out.print(e.getMessage()); } 또는
if ( input < 1 || input > 100 ) { throw new Exception(); }
catch ( Exception e ) { System.out.print("1부터 100까지 숫자를 입력하시오>>>");}
*** ObjectEquals
-> s1==s2는 참조값(주소)를 비교하는것이므로 false
-> s1.equals(s2)는 참조하는 문자열의 내용을 비교하는 것이므로 true
***MemberExample
-> Member obj1 = new Member("blue");
-> Member obj2 = new Member("blue");
-> Member obj3 = new Member("red");
-> equals메소드를 사용해 출력되는 값 확인
-> toString메소드를 사용하면 Test출력
*** Member
-> public boolean equals(Object obj) { //Object클래스의 equals메소드 재정의
if( obj instanceof Member ) { //obj타입이 Member타입인지
Member member = (Member) obj //obj를 Member타입으로 캐스팅
if(id.equals(member.id)) {//equals로 비교
return true;
} return false;
-> public String toString() { return "Test" }
*** ExceptionMethod
-> e.getMessage() : 에러 이벤트와 함께 들어오는 메세지 출력
-> e.toString() : 에러 이벤트의 toString()을 호출해서 간단한 에러 메세지 확인
-> e.printStackTrace() : 에러 메세지의 발생 근원지를 찾아 단계별로 에러 출력
'Language > Java2' 카테고리의 다른 글
Java2_day06 (0) | 2017.03.17 |
---|---|
Java2_day05 (0) | 2017.03.16 |
Java2_day04 (0) | 2017.03.16 |
Java2_day03 (0) | 2017.03.14 |
Java2_day01 (0) | 2017.03.11 |