【问题标题】:How should I fix my code so I return the correct output, a & instead of a question mark?我应该如何修复我的代码,以便返回正确的输出,一个 & 而不是问号?
【发布时间】:2014-06-19 22:47:48
【问题描述】:

对于这个任务,我们应该对 URL 进行编码。最终,输出应该是 http://www.amazon.com?id=123&author=Jim+Campbell&publisher=O%27Reilly,但我得到的是 www.amazon.com?id=123?author=Jim+Campbell?publisher=O%27Reilly

现在这是我的代码:

package MyUrl;

import java.util.Scanner;

public class MyUrl {
    private String mUrl;

    public MyUrl(String url) {

        if (url.contains("http://"))
            mUrl = "http://" + url;
        else
            mUrl = url;
    }

    public void addArgument(String name, String value) {
        if (mUrl.indexOf(name) == '?') {
            mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(value);
        } else {
            mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(value);
        }
    }

    public void addArgument(String name, int ivalue) {
        String newValue = Integer.toString(ivalue);

        if (mUrl.indexOf(name) == '?') {
            mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(newValue);
        } else {
            mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(newValue);
        }
    }

    public void addArgument(String name, double dvalue) {
        String newValue1 = Double.toString(dvalue);
        if (mUrl.indexOf(name) == '?') {
            mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(newValue1);
        } else {
            mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(newValue1);
        }
    }

    public String toString() {
        String result = mUrl;

        return result;
    }

    public static String urlEncode(String text) {
        String newWord = "";
        for (int n = 0; n < text.length(); ++n) {

            char c = text.charAt(n);
            String hexValue = Integer.toHexString(c);


            if (c >= 'A' && c <= 'Z') {
                newWord += c;
            } else if (c >= 'a' && c <= 'z') {
                newWord += c;
            } else if (c >= '0' && c <= '9') {
                newWord += c;
            } else if (c == '_' || c == '-' || c == '.' || c == '*') {
                newWord += c;
            } else if (c == ' ') {
                newWord += '+';
            } else {
                newWord += '%' + hexValue;

            }
        }
        text = newWord;
        return text;
    }

}

    /**
     * main
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Loop, asking for a new URL to be entered.
        do {
            System.out.println();
            System.out.println("Enter URL site (or 'exit')...");
            String baseUrl = sc.nextLine();
            if (baseUrl.equalsIgnoreCase("exit"))
                break;

            // Create a new MyUrl object and call its constructor
            MyUrl u = new MyUrl(baseUrl);
            System.out.println("Url value read was: " + baseUrl);

            // Loop, asking for argument/value input
            do {
                System.out.println("Enter URL argument name (or 'done')...");
                String argName = sc.nextLine();
                if (argName.equalsIgnoreCase("done"))
                    break;
                System.out.println("Enter type of argument value (string, integer, double)...");
                String argType = sc.nextLine();

                if (argType.startsWith("s")) {
                    System.out.println("Enter a string value");
                    String s = sc.nextLine();
                    u.addArgument(argName, s);
                } else if (argType.startsWith("i")) {
                    System.out.println("Enter an integer value");
                    int i = sc.nextInt();
                    sc.nextLine();
                    u.addArgument(argName, i);
                } else if (argType.startsWith("d")) {
                    System.out.println("Enter a double value");
                    double d = sc.nextDouble();
                    sc.nextLine();
                    u.addArgument(argName, d);
                } else {
                    System.out.println("Unrecognized value type - must be (s)tring, (i)nteger, or (d)ouble");
                    continue;
                }
            } while (true);

            // Display the final url
            System.out.println("URL with appended arguments is:");
            System.out.println("  " + u.toString());

        } while (true);

        // Keep console window alive until 'enter' pressed (if needed).
        System.out.println();
        System.out.println("Done - press enter key to end program");
        String junk = sc.nextLine();
    }
}

然后对于输出,我输入了所有这些,但没有得到我想要的结果。谁能告诉我我在代码中做错了什么?

输入 URL 站点(或“退出”)... www.amazon.com

读取的网址值为:www.amazon.com

输入 URL 参数名称(或“完成”)...

身份证

输入参数值的类型(字符串、整数、双精度)...

整数

输入一个整数值

123

输入 URL 参数名称(或“完成”)...

作者

输入参数值的类型(字符串、整数、双精度)...

字符串

输入一个字符串值

吉姆·坎贝尔

输入 URL 参数名称(或“完成”)...

出版商

输入参数值的类型(字符串、整数、双精度)...

字符串

输入一个字符串值

奥莱利

输入 URL 参数名称(或“完成”)...

完成

带有附加参数的网址是:
www.amazon.com?id=123?author=Jim+Campbell?publisher=O%27Reilly

输入 URL 站点(或“退出”)...

【问题讨论】:

    标签: java debugging methods computer-science


    【解决方案1】:

    我注意到了这些。

    1. 在您的构造函数中,您忘记了!。替换

      if (url.contains("http://"))
      

      if (!url.contains("http://")) 
      
    2. 在所有addArgument 方法中,替换

      if(mUrl.indexOf(name)=='?')
      

      if(mUrl.indexOf('?') != -1)
      

    说明

    1. 您正在添加http://如果它已经存在,而不是相反。 ! 不合逻辑,它反转了这一点。

    2. 您试图在 URL 中查找参数的名称,然后将索引(或 -1)与 '?' 的数值进行比较。显然行不通。

    【讨论】:

    • contains 是错误的,在这里。你想要startsWith。 indexOf 很慢;您最好跟踪一个标志,告诉您是否已添加查询字符串。那,并使 mUrl 成为 StringBuilder。
    • @david 不值得麻烦 - 您正在谈论优化以节省 1 微秒。这是他最小的问题。
    • 重要的是,java 正在自动将 ?char 扩大到 int(它的 ASCII 值),在这种情况下这没有意义。
    • 不确定您是否已修复它,但还有一个问题是“?”什么时候应该是“&”。
    • 是的。第二个问题导致条件(几乎)永远不会为真,所以一个新的 ?每次都添加。
    猜你喜欢
    • 2019-12-30
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    相关资源
    最近更新 更多