Java “错误”;找不到符号方法charAt(int)“;?

Java “错误”;找不到符号方法charAt(int)“;?,java,split,charat,Java,Split,Charat,所发生的是words是一个数组,而charAt()方法是为Strings定义的。因此,您希望使用: System.out.println("Enter a sentence to get it translated into Pig Latin: "); String sentence=kb.nextLine(); String[] words = sentence.split(" "); char a=words.charAt(0); 相反,其中索引是字符串在数组中的位置。单词是字符串

所发生的是
words
是一个数组,而
charAt()
方法是为
String
s定义的。因此,您希望使用:

System.out.println("Enter a sentence to get it translated into Pig Latin: ");

String sentence=kb.nextLine();

String[] words = sentence.split(" ");

char a=words.charAt(0);

相反,其中
索引
字符串
在数组中的位置。

单词是字符串数组。尝试使用单词[索引]。拆分后,子字符串将存储如下-

words[index].charAt(0)

访问words[index].chartAt(0)将返回相应子字符串的第一个字符。

words
是一个
字符串[]
。数组类型仅具有从
对象继承的方法,即非
字符(int)
Enter = words[0]
a = words[1]
sentence = words[2]
to = words[3]
get = words[4]
it = words[5]
translated = words[6]
into = words[7]
Pig = words[8]
Latin: = words[9]