I want to pick a particular “short” word in an array of words, that is, words with at most three characters. For this example, if you are passed an array containing the strings "Mary", "had", "a", "little", "lamb" and you are asked to return the second short word, you would return "a".
import java.util.*;
public class Numbers
{
String[] words = {"Mary", "had" , "a" , "little" , "lamb"};
int n = 2;
public String Numbers;
String[] word;
{
int counter = 1;
int length = 0;
int count = 0;
for (int i = 0; i < words.length; i++)
{
length = words[i].length();
if (length <= 3)
{
count++;
**word[count] = words[i];**
}
}
String answer = word[n];
System.out.println(answer);
}
}
When I run the code, it gives me a null exception error, and I'm not sure how to fix it. The debugger told me it had to do something with the word[count] = words[i];
What is wrong with my code?
The array needs to init.
String[] word = new String[10];