So my instructor told us to submit our assignment which I've done. She has an online java compiler that checks if the answer is right. I looked at the report and it has the right outputs and everything and below is this that makes my answers wrong:


Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Assignment2.main(Assignment2.java:30)


I really have no idea whats wrong.
Here's my code:

import java.util.Scanner;  // uses the Scanner package located in the "java.util" directory

public class Assignment2
 {
  public static void main (String[] args)
   {

     Scanner console = new Scanner(System.in);  // creates a new scanner

	int num;  //initializing all variables and values
	int max = 0;
	int pos_sum = 0;
	int odd_sum = 0;
    int evens = 0;

	while (true) // "while this is true" execute the if statements
	{


	 num = console.nextInt(); // num is scanned through scan as a Integer



	 if(num > max)  // This calculates whether or not the integer is the greatest amount
	 {
	 max = num;  // sets max integer equal to the number
	 }


	 if(num > 0)  // Adds the number to the sum if the integer is positive.
	 {
	 pos_sum = pos_sum + num;
	 }


	 if (num % 2 != 0)  // Adds the integer to the sum of odds if the number is odd
	 {
	 odd_sum = odd_sum + num;
	 }


	 if (num % 2 == 0)  // This counts the number of even numbers
	 {
	 evens++;
	 }


// prints out the statements and the following information
 System.out.println("The maximum integer is " + max + "\n"
                   + "The sum of the positive integers is " + pos_sum + "\n"
                   + "The sum of the odd integers is " + odd_sum + "\n"
                   + "The count of even integers in the sequence is " + evens + "\n");

}



}


}
Member Avatar for coil

while(true) is an infinite loop. I'm guessing eventually, there is no "nextInt()" and thus the exception is thrown. Replace it with something that actually terminates, perhaps if(s.hasNext()) .

Reading the API doc for the nextInt method says it throws:

NoSuchElementException - if input is exhausted

Coil is right. If you use a while (true) you need to get out of the loop by some means. There are two graceful ways. One would be to break on some condition, the other would be to return from the method you're in.

Ungraceful ways include System.exit() or throwing some exception which is caught above you. Another ungraceful method would be to put a try block around the next(), and put a break in the catch. These are all ugly and should be avoided. I mention them because you'll see people using them trying to be clever - they're not clever, usually.

while (console.hasNext()){}

is the standard idiom for this, because it works and it's intuitive. No reason to get fancy in this one.


You should probably get used to reading that stack trace, it'll help you find your errors. Any guesses as to what all that means?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.