1

I'm running a rather straight-forward method, that adds words to a list in alphabetical order.

For some reason, whenever the "addToData" method reaches its end, it wont return to its original caller in the main method, but instead just stops all together.

I've debugged the code, and it doesn't enter any infinite loops, or gets stuck with exceptions.

Main Method:

// Create the first list, with empty constructor
    TextList list0 = new TextList();
    
    // Check the method - addToData 
    System.out.println(list0);  
    list0.addToData("hello");
    System.out.println("list0 after adding the word hello: \n"+list0); 
    list0.addToData("hello");
    System.out.println("list0 after adding the word hello twice: \n"+list0); 

The method in which im getting stuck in:

public void addToData(String word){//   O(n) -> Because we are going over the whole list only once (n times)
    if("".equals(word))
        return;
    if(_head == null) {
        _head = new WordNode(word);
        return;
    }
    WordNode temp = _head;
    int n = 0;
    WordNode ptr = temp.getNext();
    //pointer to the next word, and test on it so we'll always have a pointer to the previous word
    while(temp.getNext() != null){
        n = ptr.getWord().compareTo(word);
        if(n <= 0){
            temp.setNext(new WordNode(word));
            temp.getNext().setNext(ptr);
            return;
        }
        else {temp = ptr;
            ptr = ptr.getNext();
        }
    }
    temp.setNext(new WordNode(word));//if we went through the whole list, that means the word is the largest and comes last
}

Only thing printed to console:

list0 after adding the word hello: hello 1

Due to multiple questions, you may find the entire project code here.

I am using IntelliJ, in case it might have anything to do with the issue...

Any and all suggestions would be appreciated, as I cant seem to find anything regarding this issue.

Croompy
  • 43
  • 5
  • Can you share the main method that's calling this code, or at least the few lines of it that surround the call to this method? – Green Cloak Guy Jun 11 '21 at 14:09
  • 6
    I'm not sure if this is related, but never compare Strings like this: word == "" – Stultuske Jun 11 '21 at 14:09
  • And the exceptions are? – riorio Jun 11 '21 at 14:09
  • have you debugged your code? – Stultuske Jun 11 '21 at 14:09
  • 3
    `if(word == "")` -->> either use `if(word.equals(""))` or in case of `""` you may want to use `if(word.isEmpty())`. – Pshemo Jun 11 '21 at 14:10
  • 3
    FYI There is no such thing as a pointer in java. Everything that is not a basic type is an object handle – gkhaos Jun 11 '21 at 14:11
  • 1
    better way: if ( "".equals(word)) in case word might be null – Stultuske Jun 11 '21 at 14:11
  • "addToData" method reaches its end - it wont return anything as return type is void – sanjeevRm Jun 11 '21 at 14:12
  • I still think it is exception but you have some try catch magic in higher levels try: `try {list0.addToData("hello");} catch (Exception e) { System.out.println("Exception was thrown");}` – Kacper Jun 11 '21 at 14:16
  • 1
    @Kacper in case of an exception there would no doubt be a stacktrace – Stultuske Jun 11 '21 at 14:17
  • @gkhaos Yes, but you have to enable this – Kacper Jun 11 '21 at 14:18
  • @Kacper attempted your suggestion, still just comes to a holt with absolutely nothing to show for it. – Croompy Jun 11 '21 at 14:20
  • 2
    @Croompy Could you also say what prints to console and what is not printed? – Kacper Jun 11 '21 at 14:21
  • 3
    The comment section here is way too long, this shouldn't be a discussion. I suggest that @Croompy provides code for `WordNode` and `TextList` (which both should not depend on anything else but standard library stuff, well, and `WordNode`) so we can reproduce the problem – gkhaos Jun 11 '21 at 14:24
  • @Croompy Just from reading your code I think you're trying to do something like [this](https://stackoverflow.com/a/8725470/10402163). Maybe it helps. – Beru Jun 11 '21 at 14:25
  • @Stultuske how is `"".equals(word)` properly solving the case of `word == null`? `"".equals(null)` will return `false`, you should check if it is null or empty (either with `word.isEmpty()` to exclude whitespace-only strings, or `"".equals(word)` for string with len 0. I assume whitespace only wouldn't make that much sense here, therefore `(word == null || word.isEmpty())` should be suitable – gkhaos Jun 11 '21 at 14:30
  • @gkhaos I've added a link with all the classes & code inside. – Croompy Jun 11 '21 at 14:34
  • 1
    So as someone suggested earlier, the problem is in `List::toString`. EDIT it actually occures in `List::toString` (doesn't mean the problem needs to be located directly there) – gkhaos Jun 11 '21 at 14:48
  • @gkhaos But when im running with the debugger im not even getting there, im just stuck at the end of the add method still. Also the print method worked with no issue previously, why would it suddenly be an issue? – Croompy Jun 11 '21 at 14:49
  • @Croompy there is a loop, just because it works for 1 element, doesn't mean at all it works for several elements – gkhaos Jun 11 '21 at 14:51
  • The loop is a no-op for the 2 element case because `getNext()` will be null. It's only possibly an issue in the 3+ element case. – Roddy of the Frozen Peas Jun 11 '21 at 14:51
  • @gkhaos it won't "solve" it, but word.equals("") will throw a NullPointerException in case word is null, "".equals(word) won't – Stultuske Jun 11 '21 at 14:57

1 Answers1

4

Your problem is in TextList.toString()

public String toString(){
    if(_head == null)
        return "";
    WordNode temp = _head;
    int counter = 1;
    String word = temp.getWord() + "\t";
    while(temp.getNext() != null){
        if(temp.getWord().equals(temp.getNext().getWord()))
        counter++;
        else {
        word = word + counter + "\n";
        if(temp.getNext() != null)
            word = word + temp.getNext().getWord() + "\t";
        }
    }
    word = word + counter;
    return word;
}

If the code ever enters the while loop, there is no way for it to ever exit it. You never change either temp or its next value, meaning that the while always compares the same value every time, creating an infinite loop.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56