在阅读了 .indexOf()
之后,我试图了解它是如何工作的。我创建了一个随机字符串并尝试搜索字符 a
。
然而,在尝试了一些事情之后我得到了这个错误,尽管我总是在所有阶段声明 String
:
incompatible types: int cannot be converted to java.lang.String
非常感谢所有可以帮助我了解我哪里出错或建议正确方法的人。
public class sad
{
// instance variables - replace the example below with your own
private String stringwords;
/**
* Constructor for objects of class sad
*/
public void sad()
{
stringwords = "this is some words a cat";
}
//
public void search()
{
String a = stringwords.indexOf("a");
System.out.println(a);
}
}
最佳答案
indexOf
返回给定字符串在调用它的字符串中的索引。您不能将此返回值分配给 String
- 它必须分配给 int
:
int index = stringwords.indexOf("a");
关于Java if语句数据输入限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444470/