Reputation: 41
I am trying to count the number of times a string appears in an array, but I cannot use any type of Map. From my research, I tried the following expecting it to work:
int count = Collections.frequency(strings, search);
However, when I got an error when running the program. The error was a Null Pointer Exception error and I'm not sure how to fix that or why it happened. Any ideas on whats wrong with my code?
EDIT: Here's my full code. I can't find where the issue lies. Maybe someone else can find it import java.util.*;
public class Lab9 {
public static void main(String[] args){
Scanner kb = new Scanner (System.in);
String name = null;
List<String> strings =null;
System.out.println("How many strings do you want to enter?:");
int size = kb.nextInt();
List<String> String = getStrings(size,name);
System.out.println(String);
System.out.println("What string would you like to search for?:");
String search = kb.next();
int numofTimes = countValues (search,strings);
System.out.print("That word appears "+numofTimes+" times in the array.");
}
public static List<String> getStrings(int size,String name) {
Scanner kb = new Scanner (System.in);
int count = 0;
List<String> strings = new ArrayList<String>();
while (count != size) {
System.out.println("Enter a string:");
name = kb.nextLine();
strings.add(name);
count = count + 1;
}
return strings;
}
public static int countValues (String search, List<String> strings){
int count = Collections.frequency(strings , search);
return count;
}
}
Upvotes: 1
Views: 7385
Reputation: 9049
This is the code for Collections.frequency()
:
public static int frequency(Collection<?> c, Object o) {
int result = 0;
if (o == null) {
for (Object e : c)
if (e == null)
result++;
} else {
for (Object e : c)
if (o.equals(e))
result++;
}
return result;
}
As you can see, it is able to deal with o == null
and with c
containing null
elements. The only case it will throw a NullPointerException
is when c == null
.
Also, from the documentation:
frequency
public static int frequency(Collection c, Object o)
Throws:
- NullPointerException - if c is null
Upvotes: 0
Reputation: 1041
Do a null and size check on strings
before using it:
if (strings != null && strings.size() > 0) {
int count = Collections.frequency(strings, search);
}
If size()
returns a number larger than 0 that means your strings
has something in it and you can perform frequency()
on it.
Upvotes: 0
Reputation: 8519
You can do a linear search through the array
String strings[] = {"A","B",null,"C","A",null}; // the array contains nulls
String search = "A";
int count = 0;
for (int i = 0;i< strings.length ;i++ )
{
if(strings[i] != null)
{
if(strings[i].equals(search))
{
count++;
}
}
}
System.out.println(count);
Upvotes: 1
Reputation: 33
Check out http://www.tutorialspoint.com/java/util/collections_frequency.htm
There's a good example of the use of frequency(Collection c, Object o);
The nullpointerexception can occur if one of the values in your collection is null. Are you sure that the excpetion is at the line of your frequency?
Upvotes: 0