본문 바로가기
Java/Java with Error

[해결 방법] java.lang.Error

by HJ0216 2023. 6. 6.

 기본 환경: IDE: Eclipse, Language: Java

 

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
 
public class Main {
    public static void main(String[] args) throws Exception {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
 
        int[][] intArr = new int[9][9];
        int[][] intMax = new int[9][9];
        
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++) {
                intArr[i][j] = Integer.parseInt(br.readLine());
                if(intArr[i][j] > intMax) {
                    intMax = intArr[i][j];
                }
            }
        }
        
        bw.write(intMax + "\n");
        bw.flush();
    }
}
 
 
 

🚨 다음과 같은 오류 발생

java.lang.Error: Unresolved compilation problems: The operator > is undefined for the argument type(s) int, int[][]

Type mismatch: cannot convert from int to int[][]

 

 

발생 원인

intArr[i][j] = int, intMax = int[][] 이므로 타입이 달라 비교할 수 없음

 

 

해결 방법

타입을 하나로 변형하여 비교(여기서는 int와 int를 비교)

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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
 
public class Main {
    public static void main(String[] args) throws Exception {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
 
        int[][] intArr = new int[9][9];
        int intMax = new int[0][0];
        
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++) {
                intArr[i][j] = Integer.parseInt(br.readLine());
                if(intArr[i][j] > intMax) {
                    intMax = intArr[i][j];
                }
            }
        }
        
        bw.write(intMax + "\n");
        bw.flush();
    }
}