我在Java中工作,我想输入一个句子到字符串数组。我将其标记并确定字数。但是,我需要将每个单词添加到字符串数组中以确定是否有重复。我不确定如果在程序后期才知道字数,我不知道如何初始化我的数组。
//Declares variables
Scanner scan = new Scanner (System.in);
int withoutdup = 0, wordCount = 0;
String line, word;
StringTokenizer tokenizer;
List<String> sentence = ArrayList<String>;
//Asks user for input
System.out.println ("Please enter text. Enter DONE to finish.");
line = scan.next();
//Tokenizes the string and counts the number of character and words
while (!line.equals("DONE"))
{
tokenizer = new StringTokenizer (line);
while (tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
wordCount++;
sentence += word;
}
line = scan.next();
}
然后你应该使用一个ArrayList来代替,并在最后你仍然可以转换为一个数组... – Naytzyrhc