0


I am not sure why I am getting this error. When I run my program I get this

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)

I am only running one scanner and it's closed after this while loop. I was curious if anyone can give me a hint on how I should five it. Here is some of the code. I also made a comment on line 27 since that is where the error is occurring at i think.

        try {
        File file = new File("src/text.txt");
        File finalGrades = new File("src/nextText.txt");
        PrintWriter output = new PrintWriter(finalGrades);
        File finalGradesReport = new File("src/nextTextReport.txt");
        PrintWriter output2 = new PrintWriter(finalGradesReport);
        Scanner input = new Scanner(file);

        double totalPointsAvailable = input.nextDouble();
        double homeworkWeight = input.nextDouble() / totalPointsAvailable;
        double projectsWeight = input.nextDouble() / totalPointsAvailable;
        double firstExamWeight = input.nextDouble() / totalPointsAvailable;
        double secondExamWeight = input.nextDouble() / totalPointsAvailable;
        double finalExamWeight = input.nextDouble() / totalPointsAvailable;

        int numA = 0, numB = 0, numC = 0, numD = 0, numF = 0, numStudents = 0;
        double totalPercGrades = 0, averageGrades = 0;

        while (input.hasNext()) {
            double hw = input.nextDouble() * homeworkWeight;
            double pw = input.nextDouble() * projectsWeight; //line 27
            double firstEx = input.nextDouble() * firstExamWeight;
            double secondEx = input.nextDouble() * secondExamWeight;
            double finalEx = input.nextDouble() * finalExamWeight;
Cory
  • 145
  • 4
  • 12
  • wrap your nextXXX() codes into ` while(input.hasNext()) {....}` – Haifeng Zhang Mar 06 '18 at 21:51
  • 1
    Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – jontro Mar 06 '18 at 21:51
  • You're getting this error because there aren't enough elements in your file. – shmosel Mar 06 '18 at 21:51
  • Possible duplicate of [NoSuchElementException with Java.Util.Scanner](https://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner) – Jacob G. Mar 06 '18 at 21:52

1 Answers1

1

The error is occurring for two reason.

  1. You are not checking if there is enough input before calling on nextDouble().

  2. hasNext() checks for the next token but is not incremented by calling nextDouble(). You want to use hasNextDouble()

You should wrap nextDouble() in a while loop and use a counter or just a conditional before each nextDouble() to make sure that there is a double token in your file and to keep track of where you are in the file

Something like this should be used:

//...below Scanner input = new Scanner(file); 
int tokenCounter = 0;

//This will set all of your variables on static fields.
while (input.hasNextDouble()) { /* <-- this wraps your nextDouble() call */
  setVariables(input.nextDouble(), tokenCounter++);
}

//several lines of code later 

public static void setVariables(double inputValue, tokenCounter){

  switch(tokenCounter){
   case 0: totalPointsAvailable = inputValue; break;
   case 1: homeworkWeight = inputValue; break;
   case 2: projectsWeight = inputValue; break;
   //Continue the switch/case based on the expected order of doubles
   //and make your variables static sense they seem changes to your code might
   //cause you to break up your main method and static variables will be easier 
   //to access across different methods.
  } 
}
Thatalent
  • 404
  • 2
  • 8