Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting String in an Array

Status
Not open for further replies.

JohnCR

Technical User
Jun 4, 2004
39
0
0
US
Hello All,

I'm hoping someone can help here. I'm trying to count how many times a string occurs in an array. Here is the code I have ...

String animals[] = {"Cat", "Cat", "Dog", "Dog", "Fish"};
String animalSingle[] = {"Cat", "Dog", "Fish"};
count = 0;
for (int i = 0; i < animalSingle.length; i++)
{
System.out.print(animalSingle);
for (j = 0; j < animals.length; j++)
{
if (animals[j] == animalSingle)
{
System.out.print(" " + j + " ");
}
else
{

}
}
}

This will display the animal and the elements that the animal appears, such as this;

Cat 0 1 Dog 2 3 Fish 4

However, I'm looking for how many times each animal appears in the array. So the output will look like this;

Cat 2 Dog 2 Fish 1

Can someone point me in the right direction? I believe it should just be a matter of counting something but I'm not sure what.

Thanks
 
Code:
public class CountAnimal {
	public static void main(String arugs[]) {
		String animals[] = { "Cat", "Cat", "Dog", "Dog", "Fish" };
		String animalSingle[] = { "Cat", "Dog", "Fish" };
		int countCat = 0;
		int countDog = 0;
		int countFish = 0;
		for (int i = 0; i < animalSingle.length; i++) {
			System.out.print(animalSingle[i]);
			for (int j = 0; j < animals.length; j++) {
				if (animals[j] == animalSingle[i]) {
					countCat = animalSingle[i].equals("Cat")?countCat+1:countCat+0;
					countDog = animalSingle[i].equals("Dog")?countDog+1:countDog+0;
					countFish = animalSingle[i].equals("Fish")?countFish+1:countFish+0;
					System.out.print(" " + j + " ");
				}
			}
		}
		System.out.println("\nCats:" + countCat);
		System.out.println("Dogs:" + countDog);
		System.out.println("Fishs:" + countFish);
	}
}

Chinese Java Faq Forum
 
This is a job for a HashMap.

1.- Loop throught the Strings
2.- Add them as keys in the HashMap
3.- As values, add an Integer and increment it on each ocurrence.
4.- Loop through the HashMap and print the values.

Cheers,
Dian
 
Thanks wangdog!

I dont quite understand what this line is doing ...

countCat = animalSingle.equals("Cat")?countCat+1:countCat+0;

Specifically, I dont understand this ....

?countCat+1:countCat+0;

Also, I'm seriously confused about the whole array/arrayList/vector/hash things. There are a lot of options to choose from.

Here's what I'm trying to do. I have a data file and I read the first three characters of each line in that file (I've got this part). Now, the data that I read will be like "AAA BBB CCC AAA DDD BBB" and so on. This can be 30 lines of data or it can be 230 lines of data (So, I would assume an ArrayList because of the Dynamic Sizing capability, is this correct?)

Now then, once I get the first three letters of each line I want to know how many times that three character combination appears in the file.

So, do I use an Arraylist to do this? How do I compare each element to get the number of times certain data appears in the arraylist? I've tried this but it's not working;

for (int i = 0; i < strg.length; i++){
//System.out.print(strg);
for (j = 0; j < arrayList.length; j++){
if (arrayList[j] == strg){
count++;
}
}
}

Thanks in advance for you help!!

JCR
 
The == operator compares the object pointer, not the actual String. Use .equals instead.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Code:
expresion?statement1:statement2

means

Code:
if (expresion)
 statement1
else
 statement2

And consider the Hashmap option, that would work for a random set of words, not just cats and dogs.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top