[Uri] 자바 코드 질문이요

  • starspder
    starspder

    import java.util.Scanner;

    public class Example {

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int T;
        String example;
        String[] encoded_strings = {"%20", "%21", "%22","%23","%24", "%25", "%26","%27","%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f"};
        String[] special_characters = { " ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "*", "+", "`", "-", ".", "/"};
        String result;
    
        T = sc.nextInt();
    
        for(int i = 0; i < T; i++) {
            example = sc.next();
            result = example;
    
            for(int j=0; j<16; j++) {
                if(example.contains(encoded_strings[j])) {
                    result = example.replace(encoded_strings[j], special_characters[j]);
                    example = result;
                }
                else {
                    continue;
                }
    
            }
            System.out.println(example);
    
    
    
        }
    }

    }

    왜안되는지 이해가 안갑니다


    7년 전
5개의 댓글이 있습니다.
  • starspder
    starspder

    참고로 클래스이름은 나중에 Main으로 고쳤는데 오답으로 뜹니다


    7년 전 link
  • JongMan
    JongMan

    문자열 "%2f"를 (손으로) 인코딩한 뒤, 이 코드로 디코딩해보세요.


    7년 전 link
  • starspder
    starspder

    글 내용이 무슨내용인지 모르겠어요...
    import java.util.Scanner;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.net.URLEncoder;

    public class Example {

    public static void main(String[] args) throws UnsupportedEncodingException {
    
    
        Scanner sc = new Scanner(System.in);
        int T;
        String example="";
        String exp = "/";
    
        String encode = URLEncoder.encode(exp,"ASCII");
    
        String[] encoded_strings = {"%20", "%21", "%22","%23","%24", "%25", "%26","%27","%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f"};
        String[] special_characters = { " ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "*", "+", "`", "-", "."};
        T = sc.nextInt();
    
        for(int i = 0; i < T; i++) {
            example = sc.next();    
            for(int j=0; j< encoded_strings.length; j++) {
                if(encoded_strings[j].equals("%2f")) {
                    example = example.replace(encoded_strings[j], URLDecoder.decode(encode,"ASCII"));
                    continue;
                }
                 example = example.replace(encoded_strings[j], special_characters[j]);
                 }
    
            System.out.println(example);
        }
    }

    }

    이런식으로 하라는 건가요??


    7년 전 link
  • geesetower
    geesetower

    출력이 "%2f"가 나오려면 입력이 어떻게 들어와야 할까요? 그걸 생각해 본 다음에 생각한 입력에 대한 출력이 예상했던대로 나오는지 확인해보세요.


    7년 전 link
  • starspder
    starspder

    해결했습니다!! 제가 생각도 못한게 %25 앞에 실행되는 %20,%21,%22 부분은 문제가 없었지만 %25 이후로 나오는 %26, %27, %28등등은 %25가 먼저 실행되기때문에 % 남아서 뒤에 문자랑 결합해서 문제가 생겨버렸네요... %25이전이 잘나왔다해서 그 이후로 못본 제 실수네요... 도움주신분들 감사드립니다!!


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