0

I have the following method in a program of mine:

/** 
 * Returns the index of the first occurrence of the specified item.
 * If the specified item in not part of the list
 * the method indexOf returns -1
 * 
 * @param item
 * @return index of the first occurrence of the item; -1 if the word was not found.
 */
public int indexOf(String item) {   

    int index = 0;

    //While we haven't reached the end of the list
    while(head != null) {
        //If the items are equal return true
        if(head.item == item) {
            return index;           
        }
        //Set the head to the next and increment the index  
        head = head.next;
        index++;
    }
    return -1;
}

While everything looks correct to me (other than needing to change head.item == item to head.item.equals(item)), it is not giving me the correct index.

While it does give me -1 for an element that is not in the lest, every time it is returning 0 as the index for an element that is in the list and I cannot figure out for the life of me why index is not incrementing.

Any input would be greatly appreciated! Thanks

  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Always Learning Jun 03 '20 at 04:52
  • where is `head` initialized? if you call `indexOf` a second time will `head` be `null` from the last call still? – Always Learning Jun 03 '20 at 04:54
  • I was given a partially started class of type WordList (a list of strings). The only fields are a head node, tail node, and an int n (the number of words in the list). The list is made in the main method by simply appending strings to the list (the append method was also written for me already). – Brandon Bischoff Jun 03 '20 at 05:03
  • Furthermore, the append method certainly does append to the end of the list. I cannot figure out what is wrong here. – Brandon Bischoff Jun 03 '20 at 05:04
  • can you show more of your class definition? I'd like to see the `class` line itself and the instance variables (like `head` and `tail`). You don't need to include other functions though. – Always Learning Jun 03 '20 at 05:09
  • I just copied the whole thing in – Brandon Bischoff Jun 03 '20 at 05:11
  • @user207421 there are bigger problems than == for String compare here - the `head` index is being modified incorrectly – Always Learning Jun 03 '20 at 05:18

1 Answers1

1

You seem to be changing the head value when you iterate through the list in indexOf and contains. The head value should always point to the first entry in your list and only be changed if needed by append and prepend. To iterate, you should use a local variable to cycle through the list. Also you should use .equals to compare String values, not ==.

public int indexOf(String item) {   

    int index = 0;
    Node current = head;

    //While we haven't reached the end of the list
    while(current != null) {
        //If the items are equal return the index
        if(current.item.equals(item)) {
            return index;           
        }
        //Set current to the next and increment the index  
        current = current.next;
        index++;
    }
    return -1;
}
Always Learning
  • 5,510
  • 2
  • 17
  • 34
  • Believe it or not I have tried it exactly like this already and it does not make a difference. – Brandon Bischoff Jun 03 '20 at 05:05
  • did you change `==` to `.equals`? what is the type of the `head` variable? – Always Learning Jun 03 '20 at 05:06
  • Yes I did change it to .equals. The head is of type Node, another class that was already included which only includes two fields (String item and Node next). – Brandon Bischoff Jun 03 '20 at 05:08
  • I can edit my original post to include all the code if that would make it easier. – Brandon Bischoff Jun 03 '20 at 05:09
  • I adjusted my explanation. Every time you have `head = head.next` you lose the original first `Node` in the list. – Always Learning Jun 03 '20 at 05:15
  • This helped me a lot thank you! I actually had created a local variable in indexOf before asking this question but didn't realize the issue was still there because I hadn't created a local variable inside the contains method as well. Anyway thank you, I can't believe I was so stuck on such a trivial mistake. – Brandon Bischoff Jun 03 '20 at 05:19
  • you're welcome - glad my suggestion got through despite it being closed as a duplicate :) – Always Learning Jun 03 '20 at 05:21