简体   繁体   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;
                    }
                }
            }
        }
    }

I am wondering why the following error is appearing, as well as how to fix it: 我想知道为什么会出现以下错误,以及如何解决该错误:

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

note: others are appearing, but I thought it would be redundant to show those as well. 注意:其他人正在出现,但我认为也要显示这些内容是多余的。

str[i].charAt(x) is a char value, not a variable. str[i].charAt(x)char值,不是变量。 You can't assign to it. 您不能分配给它。 It's similar to writing 'a' = '1'; 类似于写'a' = '1'; .

Anyway, a String is immutable, so you can't modify its characters. 无论如何,字符串是不可变的,因此您无法修改其字符。 If you want str[i] to have a new String value, you'll have to create a new String (for example, by appending characters to a StringBuilder) and assign the new String to str[i] . 如果希望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.

相关问题 Java错误消息。 意外类型,必需变量,发现值 - Java Error message. unexpected type, required variable, found value Java意外类型错误必需变量找到值 - Java unexpected type Error Required Variable Found Value Java编译错误:意外类型要求:找到变量:值 - Java Compile Error: Unexpected Type required: variable found: value 所需的Java错误意外类型:变量; 找到:ArrayList中的值 - Java Error unexpected type required: variable; found: value in an ArrayList Java转换错误:意外类型。 必需:变量,找到:值 - Java casting error: unexpected type. required: variable, found: value 为什么我收到错误:需要意外类型:找到变量:值 - Why am I getting the Error: unexpected type required: variable found: value 错误:意外类型,必需:变量并找到:值 - Error: unexpected type, required: variable and found: value Java:“意外类型; 必需:变量; 找到:值”,尝试返回一个值 - Java: “Unexpected Type; required: Variable; found: value” by trying to return a value 类运算符错误数组:所需的意外类型:找到的变量:值 - Array of Class Operator Error: unexpected type required: variable found: value 另一个作业问题错误意外类型是必需的:变量已找到:值 - another homework question error unexpected type required:variable found:value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM