Can't figure out why my CSV file is not converting to a string when adding to trie node

47 Views Asked by At

Reading in a basic csv file, the first line,(csv), is "abacus,,,,,,,, trust,,". Trying to take out the commas and add the two words to some sort of list or array. The issue starts when I try to reference the first word on the line trie.search("abacus"). Still very new to coding but this one has me stuck.

try {
    while ((csv = br.readLine()) != null) {
        String[] words = csv.split(",");
        System.out.println(csv);
        if (words.length == 2) {
            String word = words[0].trim();
            int code = 0;
            trie.insert(word, code);
        }
    }
} catch(FileNotFoundException e) {
    e.printStackTrace();
}
        
System.out.println(trie.search("abacus"));

I tried inserting the word in several different ways within the word parameter but each time it does not work unless I type "abacus" as the parameter.

1

There are 1 best solutions below

0
Reilas On

In your code, you have the following if-conditional.

if (words.length == 2)

Your split method call is going to return 9 elements.

I believe what you are intending to use is a ",+", as you're split parameter.
The split parameter is evaluated as a regular expression pattern.

Which, in this case, a plus character is going to assimilate 1 or more of the character which precedes it.

For example, here are the array elements when a single comma is used.

0 abacus
1 
2 
3 
4 
5 
6 
7 
8  trust

And, here are the elements when a ",+" is used.

0 abacus
1  trust