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!

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

    None

    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 ++;
    }
    i++;
    } 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)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Having trouble with CSC assignment. Trying to make an array of Strings
Loading...