Skip to content
Advertisement

java.util.NoSuchElementException error on multiple while statements

Screenshot of error message

Getting this error when I run my code, note that it finds a problem at line 37, but I cannot figure out what it is. Running the first iteration of the scanner method (for input 1) worked fine, and yielded the proper output, but none of the consecutive ones have, and I’ve been stuck on that issue. Code is below:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class Assignment2 {

public static void main(String[] args)  {
        
        int largest= 0;
        int largestEven = 0;
        int countOfAllPositive = 0;
        int sumOfAll = 0;
        
    try {
        Scanner input1 = new Scanner(new File("input1.txt"));
        while (input1.hasNextInt()) { 
            if (input1.nextInt() == 0)
            { break; 
            } else if (input1.nextInt() < largest)
            { largest = input1.nextInt(); 
            } else if (input1.nextInt() > 0)
            { largestEven += input1.nextInt();
            } else if (input1.nextInt() % 2 == 0)
            { countOfAllPositive += input1.nextInt();
            } else if (input1.nextInt() < 0)
            { sumOfAll++;
            }
            
        }
        Scanner input2 = new Scanner(new File("input2.txt"));
        while (input2.hasNextInt()) { 
            if (input2.nextInt() == 0)
            { break; 
            } else if (input2.nextInt() < largest)
            { largest = input2.nextInt(); 
            } else if (input2.nextInt() > 0)
            { largestEven += input2.nextInt();
            } else if (input2.nextInt() % 2 == 0)
            { countOfAllPositive += input2.nextInt();
            } else if (input2.nextInt() < 0)
            { sumOfAll++;
            }
        }
        Scanner input3 = new Scanner(new File("input3.txt"));
        while (input3.hasNextInt()) { 
            if (input3.nextInt() == 0)
            { break; 
            } else if (input3.nextInt() < largest)
            { largest = input3.nextInt(); 
            } else if (input3.nextInt() > 0)
            { largestEven += input3.nextInt();
            } else if (input3.nextInt() % 2 == 0)
            { countOfAllPositive += input3.nextInt();
            } else if (input3.nextInt() < 0)
            { sumOfAll++;
            }
        }
        Scanner input4 = new Scanner(new File("input4.txt"));
        while (input4.hasNextInt()) { 
            if (input4.nextInt() == 0)
            { break; 
            } else if (input4.nextInt() < largest)
            { largest = input4.nextInt(); 
            } else if (input4.nextInt() > 0)
            { largestEven += input4.nextInt();
            } else if (input4.nextInt() % 2 == 0)
            { countOfAllPositive += input4.nextInt();
            } else if (input4.nextInt() < 0)
            { sumOfAll++;
            }
        }
    } catch (FileNotFoundException e) {
    System.out.println("An error occurred.");
    e.printStackTrace(); } 
    
System.out.println("The largest integer in the sequence is " + largest);
System.out.println("The largest even integer in the sequence is " + largestEven);
System.out.println("The count of all positive integers in the sequence is " + countOfAllPositive);
System.out.println("The sum of all integers is " + sumOfAll);
    }
    }
    

Advertisement

Answer

The exception NoSuchElementException in your case is caused by trying to use nextInt() on a file/input that has no more int’s left to read, see the Javadoc:

Thrown by various accessor methods to indicate that the element being requested does not exist.

The reason you get the issue is because every time you call nextInt() in actually uses up an int and moves onto the next one.

So instead of this where you call nextInt() over and over in the if/else if:

if (input1.nextInt() == 0) //Gets the next int
{ break; 
} else if (input1.nextInt() < largest) //Gets the next int (different from the previous)
{ largest = input1.nextInt();  //Gets the next int (different from the previous)
... //And so on

You need to do call nextInt() once and assign it to a value then use that value:

//Store the int as a value
int value = input1.nextInt();

//Use the value instead of calling "nextInt()" again
if (value  == 0)
{ break; 
} else if (value < largest)
{ largest = value; 
...
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement