Javaif语句数据录入限制
Java if statement data entry restriction
阅读后,我正试图掌握 .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
returns 给定字符串在调用它的字符串中的索引。您不能将此 return 值分配给 String
- 它必须分配给 int
:
int index = stringwords.indexOf("a");
因为stringwords.indexOf("a");
是一个整数。您只是在问字母 a
出现在什么位置,这给出了它在数字中的位置。
例如:
String test = "Hello";
int a = test.indexOf("e");
//a = 1. First letter has the value 0, the next one 1 and so forth.
这样做:
public class sad
{
// instance variables - replace the example below with your own
private String stringwords;
/**
* Constructor for objects of class sad
*/
public sad()
{
stringwords = "this is some words a cat";
}
//
public void search()
{
int a = stringwords.indexOf("a");
System.out.println(a);
}
indexOf returns 一个字符串。查看 JavaDoc:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
public class sad
{
// instance variables - replace the example below with your own
private String stringwords;
/**
* Constructor for objects of class sad
*/
public sad()
{
stringwords = "this is some words a cat";
}
//
public void search()
{
int a = stringwords.indexOf("a");
System.out.println(a);
}
}
阅读后,我正试图掌握 .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
returns 给定字符串在调用它的字符串中的索引。您不能将此 return 值分配给 String
- 它必须分配给 int
:
int index = stringwords.indexOf("a");
因为stringwords.indexOf("a");
是一个整数。您只是在问字母 a
出现在什么位置,这给出了它在数字中的位置。
例如:
String test = "Hello";
int a = test.indexOf("e");
//a = 1. First letter has the value 0, the next one 1 and so forth.
这样做:
public class sad
{
// instance variables - replace the example below with your own
private String stringwords;
/**
* Constructor for objects of class sad
*/
public sad()
{
stringwords = "this is some words a cat";
}
//
public void search()
{
int a = stringwords.indexOf("a");
System.out.println(a);
}
indexOf returns 一个字符串。查看 JavaDoc:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
public class sad
{
// instance variables - replace the example below with your own
private String stringwords;
/**
* Constructor for objects of class sad
*/
public sad()
{
stringwords = "this is some words a cat";
}
//
public void search()
{
int a = stringwords.indexOf("a");
System.out.println(a);
}
}