Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote a program and their are 4 different test cases that it has to pass. It p

ID: 3636709 • Letter: I

Question

I wrote a program and their are 4 different test cases that it has to pass. It passes three of the four, but when I just enter '0' nothing shows up on the println.

import java.util.Scanner; // use the Scanner class located in the "java.util" directory

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


int number;

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

number = scan.nextInt(); // reads an integer entered by user
scan.hasNext();
scan.hasNext();

int minNumber=0, oddSum=0, negativeSum=0, positiveSum=0;

do
{


if(number < minNumber) // calculates whether the integer is the least
{
minNumber = number; // sets the minimum integer equal to the number
}


if(number%2 !=0) // adds the integer to the odd sum if the integer is odd
{
oddSum = oddSum number;
}


if(number < 0) // adds integer to the negative sum if the integer is negative
{
negativeSum = negativeSum number;
}

if(number > 0) // increases the positive sum by 1 if the integer is positive
{
positiveSum ;
}

}while((number = scan.nextInt()) != 0);

System.out.print("The minimum integer is " minNumber " " "The sum of the odd integers is " oddSum " "
"The sum of the negative integers is " negativeSum " " "The number of positive integers in the sequence is " positiveSum " ");


}
}

When I enter '0' everything should be '0' in the println. But instead I get this error:

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:48)


I looked this error up and it seems that the input requires more to it, but I should be able to enter as little and as much numbers as I want.

Does anyone have a solution to this problem. I have no idea what to do. Thanks!

Explanation / Answer

try this

hope you don't mind I added a prompt. and got rid of your 2 scan.next(), no reason for them

now 0 can be the first number entered

import java.util.Scanner; // use the Scanner class located in the "java.util" directory

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


int number;

Scanner scan = new Scanner(System.in); // creates a new scanner
System.out.print("Enter a number: ");
number = scan.nextInt();
int minNumber=number, oddSum=0, negativeSum=0, positiveSum=0;

while(number != 0)
{


if(number < minNumber) // calculates whether the integer is the least
{
minNumber = number; // sets the minimum integer equal to the number
}


if(number%2 !=0) // adds the integer to the odd sum if the integer is odd
{
oddSum = oddSum+number;
}


if(number < 0) // adds integer to the negative sum if the integer is negative
{
negativeSum = negativeSum+number;
}

if(number > 0) // increases the positive sum by 1 if the integer is positive
{
positiveSum++ ;
}
System.out.print("Enter a number: ");
number = scan.nextInt();
}

System.out.print("The minimum integer is "+minNumber+" "+"The sum of the odd integers is "+oddSum+" "+
"The sum of the negative integers is "+negativeSum+" "+"The number of positive integers in the sequence is "+positiveSum +" ");


}
}