티스토리 뷰

 

백준 www.acmicpc.net/problem/1303 문제를 풀다가 

NoSuchElement 에러가 발생하여 맞았는지 틀렸는지 확인이 안되는 상황이 있었다.

 

 

Before

public class Main {
    public static int N, M, countW, countB;
    public static char graph[][];
    public static boolean visited[][];
    
    static int[] moveW = new int[]{0, 1, 0, -1};
    static int[] moveH = new int[]{1, 0, -1, 0};//위, 오른쪽, 아래, 왼

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        N = Integer.parseInt(st.nextToken()); // 가로의 크기
        M = Integer.parseInt(st.nextToken()); // 세로의 크기

        graph = new char[M][N];
        visited = new boolean[M][N];
        
        for(int i=0; i<M ; i++){
            graph[i] = sc.next().toCharArray(); // 입력
        }
        ......생략
    }
}

 

After

public class Main {
    public static int N, M, countW, countB;
    public static char graph[][];
    public static boolean visited[][];
    
    static int[] moveW = new int[]{0, 1, 0, -1};
    static int[] moveH = new int[]{1, 0, -1, 0};//위, 오른쪽, 아래, 왼

    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        N = Integer.parseInt(st.nextToken()); // 가로의 크기
        M = Integer.parseInt(st.nextToken()); // 세로의 크기

        graph = new char[M][N];
        visited = new boolean[M][N];
        
        for(int i=0; i<M ; i++){
            String s = bf.readLine();
            graph[i] = s.toCharArray(); // 입력
        }
    }
}

 

Before After

        Scanner sc = new Scanner(System.in);
       
        for(int i=0; i<M ; i++){
            graph[i] = sc.next().toCharArray(); // 입력
        }

--> Scanner 선언 부분 삭제.
      이미 선언된 BufferedReader를 사용하여 입력받아야 함.       
 
        for(int i=0; i<M ; i++){
            String s = bf.readLine();
            graph[i] = s.toCharArray(); // 입력
        }

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함