0

I'm working on a program that reads a list of reviews and categorizes them based on their score and the words used in the reviews. The words scanned are based on a set of user inputs. All variables are initialized correctly. Here is the snippet:

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

However, I ran the debugger, and it said that currWord had no value, and linescan.hasNext() was false and skipped the while loop. How can I fix this?

  • What you're doing, just to make sure, is to read a line into currword (which should be currWord by Java naming conventions). Then you're making it lower case. Then you're using the scanner linescan (should be lineScan) to read the the string currword. Unfortunately currword is empty, so the while loop is avoided. – NomadMaker Oct 23 '20 at 21:02

0 Answers0