如何比较字符串中的一个字符与另一个字符串

时间:2013-02-05 10:50:05

标签: java string hex decimal

  

可能重复:
  How do I compare strings in Java?

我对Java很新,并且练习我正在尝试创建一个十六进制到十进制数转换器,因为我已成功设法生成二进制到十进制转换器。

我遇到的问题基本上是将String中的给定字符与另一个字符串进行比较。这就是我如何定义要比较的当前字符:

String current = String.valueOf(hex.charAt(i));

这是我尝试比较角色的方式:

else if (current == "b") 
   dec += 10 * (int)Math.pow(16, power);

当我尝试通过仅输入数字来运行代码时,例如12,它有效,但当我尝试使用'b'时,我得到一个奇怪的错误。以下是运行程序的完整结果:

run:
Hello! Please enter a hexadecimal number.
2b
For input string: "b" // this is the weird error I don't understand
BUILD SUCCESSFUL (total time: 1 second)

以下是仅使用数字转换成功运行程序的示例:

run:
Hello! Please enter a hexadecimal number.
22
22 in decimal: 34 // works fine
BUILD SUCCESSFUL (total time: 3 seconds)

对此表示感谢,谢谢。

编辑:我认为如果我把整个方法放在这里会很有用。

编辑2:已解决!我不知道我应该接受谁的回答,因为他们都非常善良和乐于助人。如此矛盾。

for (int i = hex.length() - 1; i >= 0; i--) {
        String lowercaseHex = hex.toLowerCase();
        char currentChar = lowercaseHex.charAt(i);

        // if numbers, multiply them by 16^current power
        if (currentChar == '0' || 
                currentChar == '1' || 
                currentChar == '2' || 
                currentChar == '3' || 
                currentChar == '4' || 
                currentChar == '5' || 
                currentChar == '6' || 
                currentChar == '7' || 
                currentChar == '8' || 
                currentChar == '9')
            // turn each number into a string then an integer, then multiply it by
            // 16 to the current power.
            dec += Integer.valueOf(String.valueOf((currentChar))) * (int)Math.pow(16, power);

        // check for letters and multiply their values by 16^current power
        else if (currentChar == 'a') 
            dec += 10 * (int)Math.pow(16, power);
        else if (currentChar == 'b') 
            dec += 11 * (int)Math.pow(16, power);
        else if (currentChar == 'c') 
            dec += 12 * (int)Math.pow(16, power);
        else if (currentChar == 'd') 
            dec += 13 * (int)Math.pow(16, power);
        else if (currentChar == 'e') 
            dec += 14 * (int)Math.pow(16, power);
        else if (currentChar == 'f') 
            dec += 15 * (int)Math.pow(16, power);
        else
            return 0;
        power++; // increment the power
    }

    return dec; // return decimal form
}

6 个答案:

答案 0 :(得分:9)

尝试将char初始化为char

返回的charAt()
char current = hex.charAt(i);

然后在条件使用中使用文字字符:

else if (current == 'b') 

由于char是基元,因此您可以使用==运算符对其进行比较。在之前的代码中,您使用String比较==,因为StringObject,代码正在检查它们是否相同Object如果它们与String.equals()方法具有相同的值,则不会。

答案 1 :(得分:1)

如果没有完整的代码,很难确定,但“for input string:xx”表单通常是与NumberFormatException相关联的消息。您是否通过使用Integer.valueOf(),Integer.parseInt()或类似方法将字符串转换为数字(部分)?

如果是这种情况,为了让它们接受十六进制数字(a-f),您需要通过调用接受基数的第二个参数的方法明确说明您传入的是16位数字 - 例如Integer.valueOf(string,16)。

另外,正如其他人已经指出的那样,比较字符串的正确方法是使用equals方法,因为如果两个引用指向完全相同的对象,则==运算符将仅返回true,而不管其值如何(内容)。

答案 2 :(得分:0)

您需要使用equals方法

比较字符串
else if ("b".equals(current)) 

您可以在此处找到更多详细信息:http://www.javabeginner.com/learn-java/java-string-comparison

答案 3 :(得分:0)

将字符串更改为char。 char current = hex.charAt(i);

然后在你的其他地方,如果你需要检查'b'和'B'

else if(current =='b'|| current =='B'){ }

答案 4 :(得分:0)

使用.equals()来比较字符串。 当你使用==我认为它会检查它们是否都引用相同的东西。

希望有所帮助。 :)

答案 5 :(得分:0)

实际上,有一种非常简单而优雅的方式:

int result = Integer.parseInt(hex, 16);
相关问题