[英]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.