0

I'm sorry. I am new to Java. I'm trying to count the length of every word in a text file, but when I print the results, every element of the String array where I store the words by length contains a null, and I really don't understand it.

import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import edu.duke.*;

public class WordLengths {

    public static void main(String[] args) {


        countWordLengths("/Users/lorenzodalberto/Downloads/ProgrammingBreakingCaesarData/smallHamlet.txt");

    }

    public static void countWordLengths(String fileName) {
        ArrayList<String> myWords = new ArrayList<String>();
        String[] wordInd = new String[20];
        int[] counts= new int[20];

        Scanner sc2 = null;

        try {
            sc2 = new Scanner(new File(fileName));
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();  
        }
        while (sc2.hasNextLine()) {
            Scanner s2 = new Scanner(sc2.nextLine());
            while (s2.hasNext()) {
                String word = s2.next();
                myWords.add(word);
            }
        }
        System.out.println("all of my words " + myWords);

        for (String word : myWords) {
            word = word.toLowerCase();
            int length = word.length();
            wordInd[length] += " " + word + " ";
            counts[length] += 1;
            System.out.println(wordInd[length]);
        }

        for (int i = 1; i < counts.length; i++) {
            int j = counts[i];  
            if (j > 0) {
                System.out.println(j + "\t words of length " + i + " " + wordInd[i]);
            }
        }
    }
}

and this is the output:

all of my words [Laer., My, necessaries, are, embark'd., Farewell., And,, sister,, as, the, winds, give, benefit]
null laer. 
null my 
null necessaries 
null are 
null embark'd. 
null embark'd.  farewell. 
null and, 
null sister, 
null my  as 
null are  the 
null laer.  winds 
null and,  give 
null sister,  benefit 
2    words of length 2 null my  as 
2    words of length 3 null are  the 
2    words of length 4 null and,  give 
2    words of length 5 null laer.  winds 
2    words of length 7 null sister,  benefit 
2    words of length 9 null embark'd.  farewell. 
1    words of length 11 null necessaries 
1
  • Arrays of Objects are filled with null. You need to put actual Strings in wordInd before you work with it. Commented Jun 13, 2017 at 8:28

2 Answers 2

2

If you add a string to null, the null is converted to the string "null". For instance, null + " hi there" gives "null hi there".

So if wordInd[length] is null, and you execute

wordInd[length] += " " + word + " ";

Then you are concatenating null to a string, giving you a string starting with "null ".

Try checking for null:

if (wordInd[length]==null) {
    wordInd[length] = word;
} else {
    wordInd[length] += " "+word;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much that solved the problem!!! I would vote you up but i have no reputation
1

When you initialize an array in Java, every empty space of the array is filled with the default value depending of the type.

Since you are creating Arrays of Strings, every slot of the array will contain a "null" value.

Your program is doing what you asked it to do: Add a space -> a new String -> another space for each new word it finds.

Edit: NVM, your question has already been answered :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.