Use Collections.frequency()
.
Example : int frequency = Collections.frequency(category, category1)
Question
I have a question on how to find the number of occurrences in a list. In my case, for part of my program, I am trying to find the number of occurrences of 1, 2, 3, and 4's of a hurricane category and displaying them. I tried using a for
loop and an if
statement but when I ran it, I got zero for the output. How can I fix this? Any help will be greatly appreciated. Below is a snippet of my program:
Hurricanes2.java:
// category 1 occurrence
int i = 0;
int ii = 0;
int category1 = category.get(i);
for(int j = 0; j < category.size(); j++){
if(category1 == 1){
ii++;
}
}
System.out.printf("%1s%10d%n", "Category 1 occurrence ~", ii);
When ran I get this:
run:
Hurricanes 1980 - 2006
Year Hurricane Category Pressure(mb) Wind Speed (mph)
____________________________________________________________________________________
1980 Allen 2 100 945
1983 Alicia 2 100 962
1984 Diana 2 100 949
1985 Bob 1 65 1002
1985 Danny 1 80 987
1985 Elena 2 100 959
1985 Gloria 1 90 942
1985 Juan 1 75 971
1985 Kate 1 85 967
1986 Bonnie 1 75 990
1986 Charley 1 65 990
1987 Floyd 1 65 993
1988 Florence 1 70 984
1989 Chantal 1 70 986
1989 Hugo 3 120 934
1989 Jerry 1 75 983
1991 Bob 1 90 962
1992 Andrew 4 145 922
1993 Emily 2 100 960
1995 Erin 1 85 973
1995 Opal 2 100 942
1996 Bertha 1 90 974
1996 Fran 2 100 954
1997 Danny 1 70 984
1998 Bonnie 1 95 964
1998 Earl 1 70 987
1998 Georges 1 90 964
1999 Bret 2 100 951
1999 Floyd 1 90 956
1999 Irene 1 70 987
2002 Lili 1 80 963
2003 Claudette 1 80 979
2003 Isabel 1 90 957
2004 Alex 1 70 972
2004 Charley 4 130 941
2004 Gaston 1 65 985
2004 Frances 1 90 960
2004 Ivan 2 105 946
2004 Jeanne 2 105 950
2005 Cindy 1 65 992
2005 Dennis 4 130 930
2005 Emily 4 135 929
2005 Irene 1 85 975
2005 Katrina 4 150 902
2005 Maria 2 100 960
2005 Nate 1 80 979
2005 Ophelia 1 80 976
2005 Phillipe 1 70 985
2005 Rita 4 150 897
2005 Stan 1 70 979
2005 Vince 1 65 987
2005 Wilma 4 150 882
2005 Beta 2 100 960
2005 Epsilon 1 75 979
2006 Ernesto 1 65 995
2006 Florence 1 80 972
2006 Gordon 2 105 955
2006 Helene 2 110 954
2006 Isaac 1 75 985
____________________________________________________________________________________
Average ~ 1 91 963
Maximum ~ 4 150 1002
Minimum ~ 1 65 882
Category 1 occurrence ~ 0
BUILD SUCCESSFUL (total time: 0 seconds)
Solution 2
Use Collections.frequency()
.
Example : int frequency = Collections.frequency(category, category1)
OTHER TIPS
Use an int[]
as a counter, like this:
// we won't use the 0 position
int[] counter = new int[5];
Iterate over all the categories. Every time you find a new category, add one to the counter:
for (int i = 0; i < category.size(); i++) {
int cat = category.get(i);
// assuming that cat is 1, 2, 3 or 4
counter[cat]++;
}
When the iteration ends, counter[1]
will hold the number of occurrences of the first category, counter[2]
will hold the number of occurrences of the second category, and so on.
you need to update what you're checking within the loop. As it stands each iteration of the loop is checking against the same value.
You probably want a
category.get(j) == 1
or
if (category.get(j) <= 4)
Move the line
int category1 = category.get(i);
down a line so that it's inside the loop, and change the i
to a j
. That way, you'll be checking a different storm's category each time, rather than just looking at the first storm over and over.