# Counting Occurrences of Integers with Arrays

Page 1 of 1

## 6 Replies - 46525 Views - Last Post: 09 September 2011 - 04:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=246606&amp;s=e4e5a3547dad42967fe004b33fcb4b5c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dmedlin

Reputation: 0
• Posts: 2
• Joined: 09-September 11

# Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 03:28 PM

Hi. I have a homework assignment that wants me to write a program that reads integers 1-100 and counts the occurrence of each then outputs in plural when applicable. I'm not currently getting any errors; however when I run the program, it allows me to enter numbers, but gives no output. I have tried so many things I can't remember all of it. I first started with two arrays and my instructor told me that was wrong. So then I redid everything I started with and now I have this. I feel like I'm so close, but I just can't get there.

package chaptersix;

import java.util.Scanner;

public class ChapterSix {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int[] num = new int[100];
int data = 0;
int sum = 0;
int times = 0;
int time = 0;
int count = 0;

System.out.print("Enter the integers between 1 and 100: ");
data = input.nextInt();
for (int i = 0; i < 100 && i > 0; i++)

while (i > 0) {
sum += data;
count++;
if (data > 1){times++;}
else
{time++;}
System.out.println(num + "occurs" + time);
System.out.println(num + "occurs" + times);

}
}
}

Is This A Good Question/Topic? 0

## Replies To: Counting Occurrences of Integers with Arrays

### #2 <3DIC

• D.I.C Regular

Reputation: 6
• Posts: 327
• Joined: 06-October 10

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 03:38 PM

You have a few issues with your code. When i compiled and ran it, it only asked me to input 1 number not 100. Do you know why? You should put

System.out.print("Enter the integers between 1 and 100: ");
data = input.nextInt();

within your for loop. Im also not sure why you have a while loop, its unnecessary. And your for loop should look like this:

for (int i = 0; i < 100; i++)

no need to check 2 conditions

this should help get you started

This post has been edited by <3DIC: 09 September 2011 - 03:43 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 04:02 PM

Start by 10 numbers first (will take more time to test)

import java.util.Scanner;

public class ChapterSix {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int nb = 10;    // <---- can be change to 100 later

int[] num = new int[nb];
for(int i = 0; i < nb; i++) {
System.out.print("Enter the integers between 1 and " + nb + ":");
int data = input.nextInt();
data--;        // have to remove 1 because arrays index start a 0
num[data]++;   // add 1 to num[0] if data entered == 1
}

// display count
for(int i = 0; i < nb; ++i) {
if(num[i] > 0)
System.out.println("There are " + num[i] + " " + (i+1));  // +1 num[0] == number of 1
}
}
}

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 04:06 PM

You may not have explained the assignment very well. This is what I think you're saying:

1. Read any number of integers 1 - 100, inclusive;
2. Count the number of occurrences of each integer in the set of entered integers;
3. Print the number of occurrences for each integer entered, (or for all integers 1 - 100?) using "time" for 1 occurrence, "times" for multiple (or zero) occurrences.

First, correct what I've gotten wrong in the above.

Assuming I'm mostly right:

For #1, you need a loop with a Scanner that accepts user input and stores the integers (in an array?) until the user enters a token that signals entry is complete.

For #2 and #3, use a loop and in if statement to count the number of occurrences of each integer from 1 - 100, inclusive, in the stored data. This can be done using a counter that is incremented for each occurrence, and is then used to output the result before it is reset to continue with the next integer.

Write the steps down to accomplish the above on paper and pencil, using plain language that you understand. When you think you've gotten all of the steps and the logic right, start programming, either fixing what you have or starting from scratch. (Starting over might be faster.)

Come back when you need more help.

### #5 dmedlin

Reputation: 0
• Posts: 2
• Joined: 09-September 11

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 04:15 PM

Thanks for the replies! Also, thanks for explaining it better than I did. That was a perfect explanation.
I redid the code based on the first reply and at least now I'm getting some output, but it's like it only reads the first number twice then quits. This is my new code:

package chaptersix;

import java.util.Scanner;

public class ChapterSix {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int[] num = new int[100];
int data = 0;
int sum = 0;
int times = 0;
int time = 0;
int count = 0;

for (int i = 0; i < 100; i++)
count++;
//      while (i > 0) {

System.out.print("Enter the integers between 1 and 100: ");
data = input.nextInt();
//       sum += data;
count++;
if (data > 1){times++;}
else
{time++;}
System.out.println(data + "occurs" + time);
System.out.println(data + "occurs" + times);

}
}

Here is what I now get when I run it:

Enter the integers between 1 and 100: 5 5 6 3 10 0
5occurs0
5occurs1
BUILD SUCCESSFUL (total time: 9 seconds)

So it's like I'm taking a step forward and two steps back. Frustrating.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 04:23 PM

This for() loop
for (int i = 0; i < 100; i++)
count++;

without any {} simply add 1 to count 100 times
if you want to enter multiple numbers on a single line you will have to

while(true) {
System.out.print("Enter number(s): ");
while(input.hasNextInt()) {
int value = input.nextInt() + 1;
num[count++]++;
if(count == 100)
break;
}
if(count == 100)
break;
}

*Edited count++ missing

This post has been edited by pbl: 09 September 2011 - 04:27 PM

### #7 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Counting Occurrences of Integers with Arrays

Posted 09 September 2011 - 04:25 PM

You're not counting the number of occurrences. How would you do it in your head?

Something like: start with the first value entered, in this case 5. Then you'd count the number of 5s: 1, 2, that's it. 2.
Repeat for each number entered, unless you've already counted it.

Hmmmmm. How do you program that part, the part where you skip a repeat number? Are you supposed to be saving the values entered by the user into an array, like the thread title says? Any idea how to do that?

Once you have the values in an array, your job will become easier.