找不到符号,Java和字符串

时间:2014-05-04 02:40:14

标签: java string

我已经尝试了一段时间来修补它并且尚未找出它给我这个错误的内容。代码远非完整,但我只想弄清楚为什么它说它找不到变量ch1。非常感谢任何帮助!

 public class PhoneNumber {
    String phoneNumber;

    public PhoneNumber(String num) {
        phoneNumber = num;
    }

    public String decodePhoneNumber() {
        // Takes string form phone number and decodes based on number pad
        // Find code that makes if statement not care about caps
        // so if a || b || c number[cnt] = 1 etc..

        for (int cnt = 0; cnt < phoneNumber.length(); cnt++) {
            char ch1 = phoneNumber.charAt(cnt);
            if (Character.ch1.equalsIgnoreCase("a") || ("b") || ("c")) {

            } else if (ch1.equalsIgnoreCase("d" || "e" || "f")) {

            } else if (ch1.equalsIgnoreCase("j" || "k" || "l")) {

            } else if (ch1.equalsIgnoreCase("m" || "n" || "o")) {

            } else if (ch1.equalsIgnoreCase("p" || "q" || "r" || "s")) {

            } else if (ch1.equalsIgnoreCase("t" || "u" || "v")) {

            } else {

            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您有语法错误,这就是您找不到ch1的原因。

尝试按照此语法修改代码。这些变化需要在所有条件中完成。

if ((ch1 == 'a') || (ch1 == 'b') || (ch1 =='c')) {

答案 1 :(得分:0)

如果你想使其工作而不管大写字母,那么你需要将输入规范化为小写,然后进行字符比较:

char ch1 = phoneNumber.toLowerCase().charAt(cnt);
if (ch1 == 'a' || ch1 == 'b' || ch1 == 'c') {
   // Do something
}
...