Find the amount of times a number appears in the array

Can someone please tell assist me on how to find the amount of times a number appears in an array. Thanks in advance for the help!
[137 byte] By [seithexa] at [2007-11-27 6:16:48]
# 1
I see a for loop and a counter.
cotton.ma at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 2

public int numTimes(int numToFind, int[] array)

{

int count = 0;

for(int i : array)

{

if(i == numToFind)

{

count++;

}

}

return count;

}

CaptainMorgan08a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 3
I am kind of new to java, just to be sure will that method find the amount of times numbers in an array appear, so if I hav an array of: {6 2 4 3 435 4 6 4 64 6} it will say:Six appears three timesTwo appears onceand so on
seithexa at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 4

> I am kind of new to java, just to be sure will that

> method find the amount of times numbers in an array

The best way to be sure of anything would be to take the method, put in code, compile it and run it and see what happens.

Please note you are going to have to make an effort to complete your own homework assignment. CaptainMorgan gave you a good start but it looks from your last post that you still have some work to do.

cotton.ma at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 5
Have you produced any code for this? Let's see what you've done and what stumbling blocks you may have hit.Good luck!
petes1234a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 6
> Six appears three times> Two appears once> and so onHave a look at the java.util.Map interface.
hwaitea at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 7

Here is what I have so far, I can check for the amount but when I get to the other number in the array it will check again and I'll have two outputs of the same number.

public class Amount{

public static void main (String [] args){

double[] abc = {6, 3, 35, 353, 55 , 6};

int count = 0;

for(int i = 0; abc[i] < abc.length; i++){

if(i > 0){

for(int a = 0; abc[a] < abc.length; a++){

if(abc[i] == abc[a]{

break;

}

}

break;

}else{

for(int b = 0; abc[b] < abc.length; b++){

if(abc[i] == abc[b]){

count++;

}

}

}

System.out.println("The Number " + abc[i] + " appears " + count + "times");

}

}

}

seithexa at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 8

public int numTimes(int numToFind, int[] array) {

String s = Arrays.toString(array);

String[] tokens = s.split("[^" + numToFind +"]+");

return tokens.length - 1;

Since we're posting solutions. However, someone will need to check my regex as it gives one too many tokens.

floundera at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 9
sorry, like i said i am pretty new to java and don't know what is happening in the code above
seithexa at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 10
I'm not really sure what you are trying to do with your code. You can still use the method I gave you, you just need to have another loop to check each number in the array.
CaptainMorgan08a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 11

I'm trying to find the amount of times a number appears in a ten number array set by the user. I can check the numbers but I'll get too many outputs because it checks checks for every number so if it check for 6 and there are two of them it will also check that 2 as well so I get two outputs saying "There are 2 2s" when I only need one

seithexa at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 12
> sorry, like i said i am pretty new to java and don't> know what is happening in the code aboveThat's ok, neither do I. Regex is on my short list of the next things I'd like to study.
petes1234a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 13
Alright, so should the program automatically output how much each number in the array appears, or does the user specify which numbers are checked?
CaptainMorgan08a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 14
> double[] abc = {6, 3, 35, 353, 55 , 6};Don't use double[] when int[] will do. This will come back to haunt you in the end. Trust me.Message was edited by: petes1234
petes1234a at 2007-7-12 17:28:53 > top of Java-index,Java Essentials,Java Programming...
# 15
It has to automatically do all numbers.
seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 16
Create a map. Iterate over array and populate map. Iterate over populated map and print to screen.
hwaitea at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 17
I have no idea what a map is, I am new to java
seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 18

> I have no idea what a map is, I am new to java

Getting started with Java

[url=http://java.sun.com/docs/books/tutorial/ ]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html ]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com ]http://javaalmanac.com [/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance ]The Java Developers Almanac[/url].

cotton.ma at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 19
My teacher gets mad if I use any coding past my current chapter, so learning maps is not an option
seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 20
> My teacher gets mad if I use any coding past my> current chapter, so learning maps is not an optionYou need to loop through every number in your array, then use the method I gave you to see how many times each number appears.
CaptainMorgan08a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 21
yes, that works, but if there is repeats it will check that number again and then output the same info twice
seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 22

> It has to automatically do all numbers.

Your example only includes ints. So it may include an array of double? The reason I ask, is that checking equality in doubles can be somewhat trickier than for ints.

Again, if there is low chance that you will be dealing with doubles (or floats), keep the array an array of ints.

petes1234a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 23
> yes, that works, but if there is repeats it will> check that number again and then output the same info> twicethen you need to correct that, don't you?
petes1234a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 24

I have tried and came up with this:

public class Amount{

public static void main (String [] args){

int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};

String[] def = new String[10];

int count = 0;

for(int i = 0; abc[i] < abc.length; i++){

for(int b = 0; abc[b] < abc.length; b++){

if(abc[i] == abc[b]){

count++;

}

}

def[i] = ("The Number " + abc[i] + " appears " + count + "times");

}

int d = 0;

for(int q = 0; q < def.length; q++){

if(def[q].equals(def[d])){

def[q] = def[d];

}

d++;

}

for(int w = 0; w < def.length; w++){

System.out.println(def[w]);

}

}

}

The problem is I get a null pointer exception. I try to put it in strings and then check the strings against each other.

seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 25

Ooh, problems here:

> for(int i = 0; abc[i] < abc.length; i++){

and here

> for(int b = 0; abc[b] < abc.length; b++){

needs to be something like

for(int i = 0; i < abc.length; i++){

and

for(int b = 0; b < abc.length; b++){

Message was edited by:

petes1234

petes1234a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 26

yeah, thanks for pointing that out, that was a huge problem, I changed it a little

public class Amount{

public static void main (String [] args){

int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};

String[] def = new String[10];

int count = 0;

for(int i = 0; i < abc.length; i++){

for(int b = 0; b < abc.length; b++){

if(abc[i] == abc[b]){

count++;

}

}

def[i] = ("The Number " + abc[i] + " appears " + count + "times");

}

int d = 1;

for(int q = 0; q < def.length; q++){

if(def[q].equals(def[d])){

def[q] = def[d];

}else{

def[q] = def[q];

}

d++;

}

for(int w = 0; w < def.length; w++){

System.out.println(def[w]);

}

}

}

EDIT: says out of bounds at line 14 and I don't see why

Message was edited by:

seithex

seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 27
Maybe you want to start d at 0, not 1?
CaptainMorgan08a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 28

The actual error message said something about "ArrayIndexOutOfBounds" and it refered you to this line:def[i] = ("The Number " + abc[i] + " appears " + count + "times");

In general you get this exception when (a) the index i is negative or (b) the index i is too big for the array.

Now i is never negative, so it's a case of the array index getting too big.

How big can i get? (Ie, what is the largest value i ever has?) Do the two array expressions - def[i] and abc[i] - make sense for this value of i?

pbrockway2a at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 29

Here is my most recent copy:

public class Amount{

public static void main (String [] args){

int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};

String[] def = new String[10];

int count = 0;

for(int i = 0; i < abc.length; i++){

for(int b = 0; b < abc.length; b++){

if(abc[i] == abc[b]){

count++;

}

}

def[i] = ("The Number " + abc[i] + " appears " + count + "times");

}

int d = 1;

for(int q = 0; q < def.length; q++){

if(d == 10){

break;

}

else if(def[q].equals(def[d])){

def[q] = def[d];

}else{

def[q] = def[q];

}

d++;

}

for(int w = 0; w < def.length; w++){

System.out.println(def[w]);

}

}

}

I still get the out of bounds, but it shouldn't be out of bounds

seithexa at 2007-7-21 21:49:26 > top of Java-index,Java Essentials,Java Programming...
# 30
> I still get the out of bounds, but it shouldn't be> out of boundsstudy pbrockway2's response. That is the key. One of those values doesn't make sense with your "i" index.
petes1234a at 2007-7-21 21:49:35 > top of Java-index,Java Essentials,Java Programming...
# 31

else if(def[q].equals(def[d])){

def[q] = def[d];

}else{

def[q] = def[q];

What is the point of this? If def[q] equals def[d] you will simply be copying the same value over itself. Same for def[q] = def[q]. Totally pointless.

floundera at 2007-7-21 21:49:35 > top of Java-index,Java Essentials,Java Programming...
# 32

One other piece of advice. You can get quick fixes here in the forum, but as a general rule, you're best sweating out most of this coding and debugging alone. I suggest that if you have a specific question that desperately needs answering, you ask it here in the forum, but then you close your internet browser and work, study, change, play, and otherwise torment your code for a good bit of time, at least 12 hours or more.

In other words: You have to work most of this out yourself in order for you to fully understand and appreciate it. Plus you'll get that sense of satisfaction when something finally goes right because of your own intelligence and perseverence. It's the same feeling that hooked most of us into coding in the first place.

petes1234a at 2007-7-21 21:49:35 > top of Java-index,Java Essentials,Java Programming...