WILDCARD문제 런타임에러(RTE (nonzero return code)) 발생질문

  • JungSol2
    JungSol2

    WILDCARD문제에서 계속 런타임에러가 발생하는데,
    원인을 모르겠습니다ㅠㅠ
    아래는 제 코드입니다!
    한번 봐주시면 감사합니다!

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
        static char[] wildCard, fileName;
        static int[][] cache;
        public static void main(String[] args) {
            Main main = new Main();
            Scanner scanner = new Scanner(System.in);
            int cases = scanner.nextInt();
            while(cases-- > 0) {
                List<String> matchedFileList = new ArrayList<>();
                wildCard = scanner.next().toCharArray();
                int fileNumber = scanner.nextInt();
                for(int i = 0; i < fileNumber; i++) {
                    String fileNameStr = scanner.next();
                    fileName = fileNameStr.toCharArray();
                    cache = new int[fileName.length][fileName.length];
                    if(main.isMatched(0, 0)) matchedFileList.add(fileNameStr);
                }
                Collections.sort(matchedFileList);
                for(String matchedFileName : matchedFileList) {
                    System.out.println(matchedFileName);
                }
            }
        }
    
        public boolean isMatched(int wildCardIndex, int fileNameIndex) {
            if(wildCardIndex == wildCard.length && fileNameIndex == fileName.length) return true;
            if(wildCardIndex == wildCard.length) return false;
            if(fileNameIndex == fileName.length) {
                return wildCard[wildCardIndex] == '*' ? isMatched(wildCardIndex+1, fileNameIndex) : false;
            }
            if(cache[wildCardIndex][fileNameIndex] != 0) return cache[wildCardIndex][fileNameIndex] == 1;
    
            boolean isMatched = false;
            switch(wildCard[wildCardIndex]) {
            case '*':
                isMatched = isMatched(wildCardIndex+1, fileNameIndex) 
                    || isMatched(wildCardIndex, fileNameIndex+1);
                break;
            case '?':
                isMatched = isMatched(wildCardIndex+1, fileNameIndex+1);
                break;
            default:
                if(wildCard[wildCardIndex] == fileName[fileNameIndex]) {
                    isMatched = isMatched(wildCardIndex+1, fileNameIndex+1);
                }
                break;
            }
            cache[wildCardIndex][fileNameIndex] = isMatched ? 1 : 2;
            return isMatched;
        }
    }
    

    6년 전
4개의 댓글이 있습니다.
  • keith
    keith

    main함수는 static으로 되어있으나, 거기서 호출하는 함수 (isMatched)는 static이 아니라서 생기는 문제입니다.
    isMatched 함수에도 static을 붙여주면 왠지 해결될거 같습니다.


    6년 전 link
  • JungSol2
    JungSol2

    그렇게 수정했으나 여전히 런타임에러 발생합니다 ㅠㅠ


    6년 전 link
  • keith
    keith

    isMatched를 Main main = new Main();에서 생성된것을 쓰지 않고 그냥, this.isMatched로 바꿔보신후, 그래도 오류가 뜬다면, 로직문제가 같은데, 아직 제가 저 문제를 풀질 않아서.. 풀고 나서 확인해 보겠습니다


    6년 전 link
  • JungSol2
    JungSol2

    Main객체로 호출하지 않고 Main.isMatched로 호출하여도 같은 문제 발생합니다 ㅠㅠ 도움 감사합니다~!


    6년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.