1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Having trouble with CSC assignment. Trying to make an array of Strings

  1. Mar 10, 2014 #1
    1. The problem statement, all variables and given/known data

    I need to write a program that takes a text file, and looks for how many unique words it has, as well as the number of times they occur in the file.

    2. Relevant equations


    3. The attempt at a solution

    public static void main(String[] args) throws FileNotFoundException {

    Scanner s = new Scanner(new File("file.txt"));

    String[] wordlist = new String[10000];
    int[] wordcount = new int[10000];
    int i = 0;
    do {
    String word = s.next();
    if (wordlist.equals(null)) {
    wordlist = word;
    wordcount ++;
    } else if (wordlist.equals(word)) {
    wordcount ++;
    } while (s.hasNext());

    when I try and do this, it compiles, but when I run it I get an error. how do I get java to check and see if an element of an array is null?
  2. jcsd
  3. Mar 11, 2014 #2
    Is this the line that gives the error?
    Code (Text):
    if (wordlist[i].equals(null)
    The problem is, of course, calling .equals() on a null element. Here's the fix:
    Code (Text):
    if(wordlist[i] == null)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted