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

时间:2014-02-02 21:04:01

标签: java split charat

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);

2 个答案:

答案 0 :(得分:1)

words是一个数组,charAt()方法是为String定义的。所以你想使用:

words[index].charAt(0)

相反,index是数组中String的位置。

答案 1 :(得分:0)

单词是一个字符串数组。尝试使用单词[index]。分割后,子字符串将存储如下 -

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]

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