[英]CharAt Function
How do I properly use the charAt function to retrieve the character of string that my user wants inputted?如何正确使用 charAt 函数来检索用户想要输入的字符串字符? I keep getting an error surrounding my (parseInt) function.
我的 (parseInt) 函数不断出现错误。
System.out.println("Choose an individual character to print out: ");
int number = 0;
try
{
number = Integer.parseInt(input.nextInt());
}
catch (IOException E)
{
System.out.println(E);
}
String aString = input.next();
char aChar = aString.charAt(number);
System.out.println ("The character you chose to print out is: " + aChar );
Javadocs useful resource: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html Javadocs 有用资源: http : //docs.oracle.com/javase/7/docs/api/java/lang/String.html
The "charAt" method takes an "int" as a parameter. “charAt”方法采用“int”作为参数。 You tell it the index position in the string you want returned.
你告诉它你想要返回的字符串中的索引位置。 Remember it is also zero based.
请记住,它也是基于零的。
For for the following:对于以下情况:
String string = "abc"
string.charAt(0).equals('a'); // true
string.charAt(1).equals('b'); // true
string.charAt(2).equals('c'); // true
string.charAt(3).equals('x'); // error, no index "3"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.