0

So i have implemented the insert method and it works just fine but my problem is how to check whether a member is already in the list or not,i want the program to check if the member is already in the list but the checker doesn't work. i want the program to put the member in team1 if the member is included in the list and Display "member does not exist" if the member is not on the list. I made a check method but it doesn't work. I am new in Programming and i really need help. Please enlighten me with your knowledge.

class Node
{
    protected String info;
    protected Node next;
    public Node(String value)
    {
        info = value;
        next = null;
    }
}
class LinkedList
{
    private Node head;
    private int count;
    public LinkedList()
    {
        head = null;
        count = 0;
    }
    public void insert( String name)
    {
        Node a = new Node(name);
        a.next = null;
        count++;
        if (head == null)
        {
            head = a;
            return;
        }
        for(Node cur = head; cur != null; cur = cur.next)
        {
            if (cur.next == null)
            {
                cur.next = a;
                return;
            }
        }
    }

    public void checker(String name)
    {

        for(Node cur = head; cur != null; cur = cur.next)
        {
            if(cur.info == name)
            {
                insertteam1(name);
                System.out.print("OK");
            }
            else
            {
                System.out.print("member does not exist");
            }
        }
    }
    public void insertteam1(String name)
    {
        Node b = new Node(name);
        b.next = null;
        count++;
        if (head == null)
        {
            head = b;
            return;
        }
        for(Node cur = head; cur != null; cur = cur.next)
        {
            if (cur.next == null)
            {
                cur.next = b;
                return;
            }

        }
    }
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    Please fix the formatting. You see instant preview below the question you are putting together. Also there's nothing specific to jcreator in your question. What is ```team1```? You should also post how you use it. And btw it is a bad idea to use a class name which is in JDK(LinkedList). – NeplatnyUdaj Mar 11 '14 at 09:51
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?lq=1) – Jason C Mar 11 '14 at 09:58
  • @user3404896 How did you determine that the check failed? What does "it doesn't work" mean and how do your expected results differ from your actual results? – Jason C Mar 11 '14 at 09:59
  • The checker doesnt work because it just prints "member does not exist" even if the name is on the list. – user3404896 Mar 11 '14 at 10:04

1 Answers1

2

In the code below,

if(cur.info == name){ // }

you are comparing the string info using == which is not the right way to compare strings in java.

Use if(cur.info.equals(name)){ // } or

use if(cur.info.equalsIgnoreCase(name)){ // } if you want to do case insensitive compare.

anirudh
  • 4,116
  • 2
  • 20
  • 35
  • I doubt this is the problem. In most cases ```==``` on Strings has the same result as equals. It is definitely a bad thing to do, but I think the problem is elsewhere. – NeplatnyUdaj Mar 11 '14 at 09:56
  • 2
    @NeplatnyUdaj It seems like the major problem to me, although there are likely others. – Jason C Mar 11 '14 at 09:57
  • 1
    @NeplatnyUdaj In most cases `==` does *not* have the same result as `equals`. It only has the same result in the case of interned string constants, or when instances of a `String` are being compared. It *never* has the same result for strings that come from external sources, or for strings that are constructed at run-time. In the *vast* majority of cases in this type of situation, `==` would cause the logic to fail. – Jason C Mar 11 '14 at 10:02
  • @NeplatnyUdaj Jason is right, `==` only compares whether the objects pointed by those references are equal and it does not compare the value stored in the string – anirudh Mar 11 '14 at 10:04
  • So I guess I have faulty JVM. – NeplatnyUdaj Mar 11 '14 at 10:06
  • 1
    @NeplatnyUdaj Your JVM works just fine. The differences between `==` and `equals()` are well-known and discussed to death and I see no reason to get into it further here. You may refer to http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?lq=1 or anywhere else for explanations. – Jason C Mar 11 '14 at 10:08
  • I know that. I just think that it might not be the solution to the OP's problem. – NeplatnyUdaj Mar 11 '14 at 10:10