java 找不到符号方法 charAt(int)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30222993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find Symbol method charAt(int)?
提问by AJ PJ
I keep recieving this error when I compile the program:
编译程序时,我不断收到此错误:
error: cannot find symbol
if(letter == charAt(0).getTheword())
symbol: method charAt(int)
The word is an arrayList and letter is a keyboard input. What am I doing wrong and what should be changed?
这个词是一个数组列表,字母是一个键盘输入。我做错了什么,应该改变什么?
回答by thatidiotguy
charAt
is a method that operates on a String.
charAt
是一种对字符串进行操作的方法。
So example usage would be:
所以示例用法是:
String s = "abc";
System.out.println(s.charAt(0)); //prints out 'a';
回答by silentprogrammer
You need to change condition to
您需要将条件更改为
if(letter == getTheword().charAt(0))
ie string should be before the method charAt()
即字符串应该在方法 charAt() 之前
回答by D. Visser
charAt()is a method that only works on Strings, as described in the documentation. It returns the char at the given index. Let's look at a simple example:
charAt()是一种仅适用于字符串的方法,如文档中所述。它返回给定索引处的字符。让我们看一个简单的例子:
String word = "Cow";
char letter = word.charAt(0);
System.out.println(letter);
This will print out the letter (char) "C" to the console, since the letter 'C' is at index 0 of the word "Cow".
这会将字母 (char) "C" 打印到控制台,因为字母 'C' 位于单词 "Cow" 的索引 0 处。
So the part where you're going wrong is that you're not specifying on which String you want to call the charAt()
method.
因此,您出错的部分是您没有指定要调用该charAt()
方法的字符串。
回答by A. Abramov
回答by Coderzz
Use this code.... Your not using a string to use charAt
使用此代码.... 您没有使用字符串来使用 charAt
import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
ArrayList<String> words = new ArrayList<String>();
words.add("Test");
char ch = sc.nextLine().charAt(0);
if(ch == words.get(0).charAt(0)){
System.out.println("Match");
}else{
System.out.println("Do not Match");
}
sc.close();
}
}