如何比较字符串中的一个字符与另一个字符串 [英] How to compare one character from a string with another string

查看:126
本文介绍了如何比较字符串中的一个字符与另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我对Java相当陌生,并且正在尝试创建一个十进制数字转换器,因为我已经成功设计了一个二进制转换为十进制转换器。

I'm fairly new to Java and to practice I'm trying to create a hexadecimal to decimal number converter since I've successfully managed to make a binary to decimal converter.

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

The problem I'm having is basically comparing a given character of in a String with another string. This is how I define the current character that is to be compared:

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

这是我如何比较字符的方法:

This is how I try to compare the character:

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

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

When I try to run the code by entering just numbers e.g. 12, it works but when I try to use a 'b', I get a weird error. Here is the entire result of running the program:

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)

以下是一个仅通过数字转换成功运行程序的示例:

Here is an example of successfully running the program with just a number conversion:

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

任何帮助都将不胜感激,谢谢。

Any help with this would be appreciated, thanks.

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

I think it will be useful if I put the entire method here.

编辑2:已解决!我不知道我应该接受谁的回答,因为他们是非常好,乐于助人。如此冲突。

Edit 2: SOLVED! I don't know who's answer that I should accept though because they were all so good and helpful. So conflicted.

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
}


推荐答案

将字符初始化为由 charAt()

char current = hex.charAt(i);

然后在您的条件中使用字符型字符:

Then in your conditional use the literal char:

else if (current == 'b') 

由于 char 是一个原语,您可以使用 == 运算符来比较它。在前面的代码中,您使用 == 来比较 String ,因为 String 是一个 Object 代码检查它们是否相同 Object 如果它们有与 String.equals()方法的值相同。

Since char is a primitive you can compare it using the == operator. In the former code you were comparing a String using ==, since a String is an Object the code is checking to see if they are the same Object not if they have the same value as the String.equals() method would.

这篇关于如何比较字符串中的一个字符与另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆