Hello i have been working on my assignment for java for a while now and I was hoping I can get some help.
My teacher makes us run test cases to ensure our code works. My code compiles successfully but does not pass any of the test cases. It would be wonderful if you could see if i did something incorrectly I am new to java and just need some help.Thank you!

Here is the promt:
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 inputted 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 even 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 even integers (can be divided by 2, "num%2 == 0") and count how many positive integers (are greater than 0) 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 code:

 {import java.util.Scanner;


public class Assignment2


public static void main (String[] args)
   {
    int num;
     Scanner in = new Scanner(System.in);
         int minint = 100 ;
            int poscount = 0;
                int possum = 0;
    while((num = in.nextInt()) != 0);
        {
            do
            {
                if(num < minint)
                {
                minint = num;
                }
                if (num % 2 == 0)
                {
                possum = possum + num;
                }
                else if (num > 0)
                {
                poscount++;
                }
            }
            while((num = in.nextInt()) != 0);
        }

 System.out.print("The minimum integer is" + minint + "\n"
                   + "The sum of even integers is" + possum + "\n"
                   + "The count of positive integers in the sequence is" + poscount + "\n");



   }
  }
Stuugie commented: Thanks for being a good student! +6

Recommended Answers

All 7 Replies

and what exactly is your specific question ?
at this point, we would have to run the code and start guessing at what it is you are having trouble with.

also: remove the '{' before the import statement, and add one after the 'public class Assignment2' line

Some things I'm noticing:

There are no test cases (Unit Tests), but I'll assume you meant "it doesn't work".

{import

Is that just a pasting error? Can't have a { there.

public class Assignment2

It needs to go at the end of the above line instead.

int minint = 100 ; so if I enter 101 and 102 as numbers? Which one will be lowest? Consider Integer.MAX_VALUE.

You have a do-while inside of a while that are both checking for that condition. Either use a while or a do-while, you'll have to think about which one to use in this case. Keep in mind the difference between the two, what's special about do-while over while. Hint: where does the check take place.

The number 0 is included in the sequence of integers and should be included in all of your calculations.

You stop looping as soon as you see the 0, so you never process that 0, so it's not included in your calculations.

The first { by import was a pasting error sorry about that, also I can post the test cases if that helps. Also a question to traevel, could you recommend which loop would be easier to work with my code?While or do while.

Oh ok I see, I would use the do while loop right? because I want to check while this case occurs ( when the number is not 0) then i want to do th process to figure out the min value and so on?

When I deleted my while loop at the end I get this error. I remember why I included the while loop at the end it was due to this error message popping up.

java:30: error: while expected
            }
             ^

Yes. A do-while will execute the processing for the last (zero) entry, which is what the spec requires.

ok so i fixed my errors i did not use a do while loop instead I just used my single while loop I noticed that I had a ; after my while loop =[ rookie mistake once I got rid of that and made some minor changes I got my code to work thank you for the help!

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.