I need to fix a minor error in my assignment. The prompt is:
Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the input numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The minimum integer is 0
The sum of the positive integers is 0
The sum of the odd integers is 0
The count of positive integers in the sequence is 0
This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, the sum of all positive integers (are greater than 0), the sum of all positive odd integers (i.e. "num%2 == 1") and also count how many positive integers in the sequence.

Note that the above is an output for the first test case. For other test cases, you will have different numbers.

Do not prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.

My issue is that my loops will not calculate for ALL the numbers. I keep leaving out 1 number when the loop hits 0.

Recommended Answers

All 3 Replies

this is my code:

import java.util.Scanner;

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

     int number;

     Scanner console = new Scanner(System.in);


     int minInt = 0;
     int pCount = 0;
     int posSum = 0;
     int oddSum = 0;
     String input;

        input = console.nextLine();
        number = Integer.parseInt(input); // call this once!


    while (number != 0){

    number = console.nextInt(); // call this once!



             if(number < minInt)
                         {
                 minInt = number;
                        }

            if(number > 0)
                        {
                posSum += number;
                        }

              if(number % 2 == 0)
                        {
                 oddSum += number;
                        }

            if(number > 0)
                        {
                pCount++;
                        }


}








System.out.print("The minimum integer is " + minInt + "\n" +
                    "The sum of the positive integers is " + posSum + "\n" +
                    "The sum of the odd integers is " + oddSum + "\n" +
                    "The count of positive integers in the sequence is " + pCount);


        }
}

Move this line of code at the end of your while loop.

number = console.nextInt(); // call this once!

// call this once

that's right. You call it twice.

the number you read first (line 18) is never used, and is immediately replaced by the second number (line 23).
All you need is to remove line 18, and initialise number to some dummy value (not zero) so the while loop will start executing.

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.