Deep_Lee
article thumbnail


HashMap에 id와 pw 모두 String 형태로 저장하면 된다.

 

공백을 기준으로 입력받으니 , 입력받은 문자를 split으로 공백을 기준으로 나누어 id와 pw를 배열형태로 나눠주고

Key : id ( 배열의[0] ) // Value : pw ( 배열의[1] ) 의 구조를 띄게 된다.

 

이후, 비밀번호를 찾으려는 사이트 주소를 입력받으면 map.get()함수를 이용해서 Value인 비밀번호를 출력해주면 된다.

 

import java.util.*;

class Main {
    public static void main(String args[]) throws Exception {
        Scanner kb = new Scanner(System.in);
        int n=kb.nextInt();
        int m=kb.nextInt();
        kb.nextLine(); // 개행처리

        HashMap<String, String> map = new HashMap<>();

        for(int i=0; i<n; i++){
            String temp=kb.nextLine();
            String[] arr=temp.split(" ");
            map.put(arr[0],arr[1]);
        }
        for(int i=0; i<m; i++){
            String search=kb.next();
            System.out.println(map.get(search));
        }
    }
}