Scanner.nextLine()和hasNext()跳过一会儿循环

时间:2020-10-23 19:44:43

标签: java java.util.scanner

我正在开发一个程序,该程序读取评论列表并根据其得分和评论中使用的单词对它们进行分类。扫描的单词基于一组用户输入。所有变量均已正确初始化。这是代码段:

System.out.print(GETWORD); 
String currWord = scan.nextLine();
String holderWord = "";
currWord = currWord.toLowerCase();
double[] totReviews = new double[currWord.length()];
totReviewNum = 0.0;
int i = 0;
Scanner linescan = new Scanner(currWord);
while (linescan.hasNext()) {
   holderWord = linescan.next();
   totReviews[i] = review(holderWord, fileName); //outputs a double reviewScore based on the number of 
                                                 // reviews in filename.txt that contain holderword
   currCounter += numLines(holderWord + ".txt"); // outputs the number of reviews with holderword
   i++;
}
for (int j = 0; j < i; j++) {
   totReviewNum += totReviews[j];
}
totReviewNum /= i;
System.out.print("average score for those reviews is ");
System.out.printf("%.5f", totReviewNum);

但是,我运行了调试器,它说currWord没有任何价值,而linescan.hasNext()为false并跳过了while循环。我该如何解决?

0 个答案:

没有答案