-1

I need the program to run in a loop until 0 is entered. My code will end with 0 entered but when attempting to run the program with numbers entered it still ends the program. instead of running the numbers entered. The while loop is to keep the program running unless a 0 is entered.

import java.util.Scanner;
public class CountCompare {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the integers between 1 and 100 (0 to end, 0 < to exit): ");
        int[] counts = new int[100];
        // Count occurrence of numbers
        count(counts);

        while(counts[0] > 0){
            // Display results
            for (int i = 0; i < counts.length; i++) {
                if (counts[i] > 0)
                    System.out.println((i + 1) + " occurs " + counts[i] +
                            " time" + (counts[i] > 1 ? "s" : ""));
            }
            System.out.print("Enter the integers between 1 and 100 : ");

            // Count occurrence of numbers
            count(counts);
        }
        System.out.print("\nEnd of run");
    }

    /** Method count reads integers between 1 and 100 
     *   and counts the occurrences of each */
    public static void count(int[] counts){
        Scanner input = new Scanner(System.in);
        int num; // holds user input
        do {
            num = input.nextInt();
            if (num >= 1 && num <= 100) 
                counts[num - 1]++;
        } while (num != 0);
    }
}

I have posted the entire program. 

output looks like this

Enter the integers between 1 and 100 (0 to end, <0 to exit): 23 23 4 5 6 7 8 0 4 occurs 1 time 5 occurs 1 time 6 occurs 1 time 7 occurs 1 time 8 occurs 1 time 23 occurs 2 times

Enter the integers between 1 and 100:

Stanmoonie
  • 31
  • 5

3 Answers3

1

Your program still ends because:

int[] counts = new int[100];

You have defined the limit of the counts here. This means your loop will run

for (int i = 0; i < counts.length; i++)// counts.length=100;

So as far as you code suggest you want to end the user input when user input 0. So you might do this:

int x=1;
int y;
    Scanner sc= new Scanner(System.in);
    while(x!=0){
    System.out.println("Enter your values");
    y=sc.nextInt();
     if(y==0){
      x=0;
}
else{
System.out.println("You entered "+y);
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
Bishal Gautam
  • 380
  • 3
  • 16
  • there is a missing "}" at the end of your code, to close the while loop. Other than that, I would add sc.close(); in the else to close the scanner, if your code runs further. – M.Hamidi May 09 '16 at 02:45
0
int count=new Scanner(System.in).nextInt();
int myarray[]=new int[count];
for(int tmp=0; tmp<count;)
    myarray[tmp]=++tmp;
while(count != 0){
    for(int inc=1; inc<=count; inc++){
        System.out.println(inc + "times occur");
    }
    System.out.println("Enter 0 to exit");
    count=new Scanner(System.in).nextInt();
}
Sumit Bhatt
  • 718
  • 4
  • 19
0

You should take a look at the line

while(counts[0] > 0) {

and try to figure out what is the purpose of this while loop in the main method.

tepe
  • 41
  • 3