[英]using charAt() function assigning to the char array is throwing error
I'm trying to save string in to a character array using the below code & im getting this error: Type mismatch: cannot convert from char to char[] 我正在尝试使用以下代码将字符串保存到字符数组中,并且我收到此错误:类型不匹配:无法从char转换为char []
Below is the code 下面是代码
public class ExCaluculator{
char[] swi=new char[10];
String San="hello world" ;
int samsu ;
public void excal(){
for(samsu=0;samsu>San.length();samsu++)
{
swi=San.charAt(samsu);
}
}
}
Also please suggest me any other methods to do the same 还请建议我其他方法可以做到这一点
ou have to assign the value to an element of the array: 您必须将值分配给数组的元素:
public class ExCaluculator{
char[] swi=new char[10];
String San="hello world" ;
int samsu ;
public void excal(){
for(samsu=0;samsu>2;samsu++)
{
swi[samsu]=San.charAt(samsu);
}
}
}
You can't assign a char
to char[]
, array is subscript based. 您无法将
char
分配给char[]
,数组基于下标。 use something like 使用类似
swi[0] = San.charAt(samsu);
Array
must be accessed with an index
and you need a change in condition
of for
statement. 必须使用
index
访问Array
,并且您需要更改for
语句的condition
。
for(samsu=0;samsu<San.length();samsu++)
{
swi[samsu]=San.charAt(samsu);
}
Also you have to increase the array
size to 11
( char[] swi = new char[11];
) in order to avoid ArrayIndexOutOfBoundsException
. 另外,还必须将
array
大小增加到11
( char[] swi = new char[11];
),以避免ArrayIndexOutOfBoundsException
。
Or Simply you can use 或者您可以简单地使用
char[] xyz = san.toCharArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.