[英]cannot convert int to string array
Hi the problem in the code is at the end finalResult[index]= mark;嗨,代码中的问题在最后 finalResult[index]= mark; code, mark gives error.
代码,标记给出错误。 It says cannot convert int to string.
它说不能将int转换为字符串。 How can i fix it?
我该如何解决?
System.out.println("Please choose a criteria (2-7) ?");
topic = in.nextInt();
System.out.println("Please enter a mark :");
int mark = in.nextInt();
final int size = cols.length;
String[] finalResult = new String[size];
int index = 0;
while(index<finalResult.length ) {
if (index==topic) {
finalResult[index]= mark;
} else {
finalResult[index]=cols[index];
}
index++;
}
}
The problem is here:问题在这里:
finalResult[index] = mark;
You can't put int number to String array :您不能将int number放入String 数组:
Error:
错误:
incompatible types: int cannot be converted to java.lang.String不兼容的类型:int 无法转换为 java.lang.String
You need to convert number to String before adding to the array .在添加到数组之前,您需要将数字转换为字符串。
You have to change as follows:您必须进行如下更改:
finalResult[index] = String.valueOf(mark);
or或者
finalResult[index] = mark + "";
Casting integer to string is done with method String.valueOf
so in your case it would be将 integer 转换为字符串是使用方法
String.valueOf
完成的,所以在你的情况下它会是
String.valueOf(mark)
Change the mark declaration in the code as below,更改代码中的标记声明如下,
String mark = in.nextLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.