0

I was given an assignment to write a program which will accept any number of input data until 999 has been read. Then the program should type out total number of zero's and various other requests, but the problem is I don't know the output command to tell it to read the number of zeros. All I have so far is

 import java.util.Scanner;
 public class MidtermI {
    public static void main(String args[]) {
        Scanner console = new Scanner(System.in);
        int numbers = console.nextInt();

and then from there I'm lost.

1

2 Answers 2

2

Break it down into small steps, and check that each step works before going further:

The first thing you need to sort out is to be able to read multiple inputs, by looping. Your current code only reads a single number from the Scanner.

Next, you need to check for the special value 999, and stop looping when that is received.

When you have got that working, figure out how to count the zeros - either by counting them as they arrive, or collecting all the values and counting them afterwards.

You can then print out the required results using System.out.println() - but you'll probably want to use that for testing and debugging your code as you go along, anyway.

Sign up to request clarification or add additional context in comments.

1 Comment

Thoroughly helpful post, without spoon-feeding. Very nice!
1

Create ArrayList to hold the zero values the print its size.

ArrayList<Integer> zeroValues = new ArrayList<Integer>();

Then loop n time to input n numbers:

for(int i=0; i<999; i++) {
    int numbers = console.nextInt();
    if(number == 0) {
        zeroValues.add(number);
    }
}

Then you could print the total of zero's values like:

System.out.println(zeroValues.size());

1 Comment

Thanks, I never used an array list so I didn't think of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.