Boolean 관련 메소드를 실습했다.
String foo = "hello world";
System.out.println(foo.contains("world"));
System.out.println(foo.contains("aaa.."));
"contains"
foo 라는 변수 안에 해당 문자열이 포함되어 있는지,
있다면 true, 아니라면 false를 반환하는 메소드
System.out.println(1<1); //false
System.out.println(1>=1); //true
public class AuthApp {
public static void main(String[] args) {
System.out.println(args[0]);
String id = "jujuclub";
String inputId = args[0];
String passWord = "1111";
String inputPass = args[1];
System.out.println("hi");
if(inputId.equals(id) && inputPass.equals(passWord)) {
System.out.println("Master!");
} else {
System.out.println("Who are you?");
}
}
}
위 사진 처럼 argument를 작성하면 args[0],[1]을 구분하여 할당됨
inputId.equals(id) && inputPass.equals(passWord)
&& 논리연산자를 사용해서 args속 아이디와 비번이 일치하는지 확인 후 결과를 출력하는 프로그램을 작성했다.
primitive 원시데이터타입, non primitive 비원시데이터타입 에따라서
==동등비교연산자, equals() 메소드 사용이 나뉜다.
==동등비교연산자의 경우, 원시데이터타입처럼 같은값이 같은위치에저장된경우 사용
equals()메소드의 경우, 비원시데이터타입처럼 새로운 다른위치에 저장되었지만 같은내용인지를 비교하는경우 사용
String[] users = new String[3];
users[0] = "egoing";
users[1] = "jinhuck";
users[2] = "youbin";
for(int i=0; i<users.length; i++) {
System.out.println(users[i]+",");
}
for 문속 i는 length를 사용하는 것이 적합하다
public static void main(String[] args) {
String[][] users = {
{"egoing","1111"},
{"jinhuck","2222"},
{"youbin","3333"}
};
String inputId = args[0];
String inputPass = args[1];
boolean isLogined = false;
boolean isWrongId = false; // 플래그변수
boolean isWrongPass = false;
for(int i = 0; i<users.length; i++) {
String[] current = users[i];
if(current[0].equals(inputId)&¤t[1].equals(inputPass)){
isLogined = true;
break;
} else if (current[0].equals(inputId)&&!(current[1].equals(inputPass))){
isWrongPass = true;
break;
} else if (!(current[0].equals(inputId))&¤t[1].equals(inputPass)){
isWrongId = true;
break;
}
}
if(isLogined) {
System.out.println("로그인 성공!");
}else if(isWrongId){
System.out.println("존재 하지않는 아이디입니다");
}else if(isWrongPass){
System.out.println("비밀번호가 틀립니다");
}
}
inputId,pass에 입력된 아이디와 비밀번호를 저장하고,
상황에 따른 boolean형 플래그변수를 3개 만들었다.
문자열 배열 형 current 변수에 users[i]를 저장해서 비교할 준비를 하고
for문에서 user배열 수(인원수) 만큼 하나 하나 검사를 한다.
current[0].equals(inputId)&¤t[1].equals(inputPass)
inputId와 current[0](이는 아이디에 해당)가 일치한가? 그리고(&&)
inputPass와 current[1](비번에 해당)가 일치한가?
그렇다면 isLogined가 참이므로 로그인에 성공한다.
inputId와 current[0]가 일치한가? 그리고(&&)
inputPass와 current[1]가 서로 다른가?
그렇다면 isWrongId가 참이므로 존재하지 않는 아이디로 출력한다.
inputId와 current[0]가 서로 다른가? 그리고(&&)
inputPass와 current[1]가 일치한가?
그렇다면 isWrongPass가 참이므로 비밀번호가 틀렸다를 출력한다.