RTE(nonzero return code)의 발생 이유

  • 이지민
    이지민

    튜토리얼 - URI

    런타임 오류가 나는 이유를 모르겠습니다.
    검색해서 관련 글들을 읽었음에도 불구하고 도통 못 찾겠습니다.
    제가 질문드리고 싶은 것은 크게 두가지 입니다.

    1. 런타임 오류가 발생하는 이유(입력이나 출력 등 런타임 오류를 발생시키는 특정한 원인이 알고 싶습니다.)
    2. 아래 코드에서 해당 오류를 발생시키는 부분

    패키지 지정도 되어 있지 않고 메인 함수의 이름도 Main으로 되어 있으며 Scanner를 써서 nextLine 이용해서 읽어오는 것까지 처음 예제와 같은데도 왜 나는 걸까요

    import java.util.Scanner;

    public class Main {
    public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int testCase = 0;
    String uri;

    testCase = Integer.parseInt(scan.nextLine());
    
        while(testCase-- > 0){
            uri = null;                         //초기화
            uri = scan.nextLine();
    
            uri = uri.replaceAll("%20", " ");
            uri = uri.replaceAll("%21", "!");
            uri = uri.replaceAll("%24", "$");
            uri = uri.replaceAll("%25", "##"); //변경 후 최종 변경
            uri = uri.replaceAll("%28", "(");
            uri = uri.replaceAll("%29", ")");
            uri = uri.replaceAll("%2a", "*");
    
            uri = uri.replaceAll("##", "%");
            System.out.println(uri);
        }
        scan.close();
    }

    }


    8년 전
4개의 댓글이 있습니다.
  • magic4424
    magic4424

    메인코드가 0을 반환하지 않아서 생기는 오류 같네여
    void main말고 int main으로 해보시는게?


    8년 전 link
  • 이지민
    이지민

    main으로 변경하고 마지막에 return 0; 를 넣어도 똑같은 런타임에러가 나네요ㅜ 패스해 뒀다가 좀 더 고민해봐야겠습니다.
    답변 감사합니다!


    8년 전 link
  • coco
    coco

    자바 runtime error 같은 경우에는 java.lang.RuntimeException 에 속하는 걸 말하는데, 이 에러 같은 경우 따로 catch하지 않아도 프로그램은 실행되는데. exception이 발생하면 프로그램이 꺼져요. RuntimeException 종류는 java API찾아 보시면 알 수 있구, array 잘못 접근하는 경우, class casting 잘못한 경우 등등 발생합니다. 해당 코드에서 발생할만한 RuntimeException은 java.util.regex.PatternSyntaxException로써 replaceAll()에서 발생하는 exception입니다.

    replaceAll
    public String replaceAll(String regex,
    String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement.
    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

    Pattern.compile(regex).matcher(str).replaceAll(repl)
    Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

    Parameters:
    regex - the regular expression to which this string is to be matched
    replacement - the string to be substituted for each match
    Returns:
    The resulting String
    Throws:
    PatternSyntaxException - if the regular expression's syntax is invalid
    Since:
    1.4
    See Also:
    Pattern

    윗 부분에서 Note that backslashes () and dollar signs () in the replacement string may cause the results to be different than if it were being treated as a literal replacement string ~~~
    읽어보시면,사용할때 literal로 취급하게 바꾸라고 되있기 때문에 ,
    uri = uri.replaceAll("%24", "
    ");
    −>uri = uri.replaceAll("%24", Matcher.quoteReplacement("$")); 로 변경하시면 정상적으로 작동할겁니다.


    8년 전 link
  • coco
    coco

    아랫 부분 이상한게 왜 나왔는지 모르겠네요 -_-
    원래 c 유저라서, java로 제출할때 이름이 Main만 가능한지 몰라서 runtime error 계속 나와서 뻘짓했다는....


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