繁体   English   中英

Java:设置字符串中字符位置的值时,我收到意外的类型错误(必填:变量;找到:值)

[英]Java: I am receiving an unexpected type error(required: variable; found: value) when setting the value of a character location in a string

public static void octIn(String[] input, String octString)
    {
        String strString, biString;
        String[] str = new String[2];
        //Octal to Binary
        biString = Integer.toBinaryString(Integer.parseInt(octString, 8));
        //Binary to String
        String[] bi = splitBi(biString);
        for (int i = 0; i < 3; i++)
        {
            for (int x = 0; x < 3; x++)
            {
                if (bi[i].charAt(x) == 0)
                    str[i].charAt(x) = '-';
                else 
                {
                    switch(x)
                    {
                        case 0:
                            str[i].charAt(x) = 'r';
                            break;
                        case 1:
                            str[i].charAt(x) = 'w';
                            break;
                        case 2:
                            str[i].charAt(x) = 'x';
                            break;
                    }
                }
            }
        }
    }

我想知道为什么会出现以下错误,以及如何解决该错误:

ACSL1.java:36: error: unexpected type
                bi[i].charAt(x) = '1';
                            ^
  required: variable
  found:    value

注意:其他人正在出现,但我认为也要显示这些内容是多余的。

str[i].charAt(x)char值,不是变量。 您不能分配给它。 类似于写'a' = '1';

无论如何,字符串是不可变的,因此您无法修改其字符。 如果希望str[i]具有新的String值,则必须创建一个新的String(例如,通过将字符附加到StringBuilder)并将新的String分配给str[i]

    for (int i = 0; i < 3; i++)
    {
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < 3; x++)
        {
            if (bi[i].charAt(x) == 0)
                sb.append('-');
            else 
            {
                switch(x)
                {
                    case 0:
                        sb.append('r');
                        break;
                    case 1:
                        sb.append('w');
                        break;
                    case 2:
                        sb.append('x');
                        break;
                }
            }
        }
        str[i] = sb.toString();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM