Hi,
I am implementing Linked List in Java. I am using three classes for this i.e Data Class, Node Class and Linked List Class. Now the problem is that a searchNode method in my linked List class doesn't work properly. I have tried to debug it it, but can't understand. Please if you can help me.

The code is

1. The Data Class

class Data {
   
    private String name;  //String which is the Data Part of the Node
   
    public Data(){
    	name=null;
    }
    
    public Data(String str){
    	name=str;
    }
    //Mutator method for setting the string
    public void setData(String str){
        name=str;
    }
    //Mutator method for getting the string
    public String getData(){
        return (name);
    }
       
}

2. The Node Class

/*This is the Node Class Which creates the Nodes*/



public class Node{
    private Data data; //The Data Object to hold data part of the Node
    private Node next; // The Node Object which holds reference to the next Node
   
    //default constructor for Node
    public  Node(){
       
        next=null;
    }
    
    public Node(Data data, Node next){
    	this.data=data;
    	this.next=next;
    }
    //Method for setting the Node's Data
    public void setData(Data data)
    {
        this.data=data;
    }
   
    //Method for setting the pointing part of the Node
    public void setNext(Node next){
        this.next=next;
    }
    //Method for getting the Node's Data
    public Data getData(){
        return (data);
    }
    //Method for getting the Nodes' pointing part
    public Node getNext(){
        return (next);
    }
   
    /*public static void main(String args[])
    {
        System.out.println("Hay I am Successful");
    }*/
}

3. The Liked List Class

import java.io.*;

public class LinkList{
    private Node firstNode,lastNode;
   
   
    public LinkList(){
        firstNode=null;
        lastNode=null;
    }
   
    public void setLastNode(Node node)
    {
        lastNode=node;
    }
    public void setFirstNode(Node node)
    {
        firstNode=node;
    }
    public Node getFirstNode()
    {
        return (firstNode);
    }
    public Node getLastNode()
    {
        return (lastNode);
    }
   
    public Node createNode(Data data){
        Node newNode=new Node(data,null);
        //newNode.setData(data);
        //newNode.setNext(null);
       
        return (newNode);
    }
    public boolean emptyList(){
    	
		if(this.getFirstNode()==null){
			return true;
		}
		else{
			return false;
		}
  }
   
    public void insertNode(Node node)
    {
        if(this.emptyList())
        {
            this.setFirstNode(node);
            this.setLastNode(node);
        }
       
        else
        {
            Node temp;
            temp=this.getFirstNode();
           
            while(temp.getNext()!=null){
                temp=temp.getNext();
            }
            temp.setNext(node);
            this.setLastNode(node);
           
        }
    }
    
   public String searchNode(String str){
	   
	   String notFound="Not Found";
	   if(this.emptyList()){
		   return (notFound);
	   }
	   else{
		   
		   Node temp=this.getFirstNode();
		   while(temp!=null){
			   		if(temp.getData().getData()==str)
			   			notFound=str;
			   		else
			   			temp=temp.getNext();
			   }
			   
	   }
			  
		return (notFound);   
		  
	   }
	  	
		  
 
	  
	    
   
    	 
    public void showList()
    {
    	if(this.emptyList()){
    		System.out.println("List is Empty");
    	}
       
        Node temp;
        temp=this.getFirstNode();
       
        while(temp!=null)
        {
            System.out.println("The Current Node is :"+temp.getData().getData());
            System.out.println();
            temp=temp.getNext();
        }
       
    }
   
   public static void main(String args[]) throws IOException
    {
        LinkList myList=new LinkList();
        BufferedReader consoleInput=new BufferedReader(new InputStreamReader(System.in));
        Data data=new Data();
        Node newNode=new Node();
        String input;
        System.out.println("Enter Some Names");
        System.out.println("Enter stope to quite");
        
        while ((input = consoleInput.readLine()).equalsIgnoreCase("stop") == false) {
            Data d = new Data();
            d.setData(input);
            myList.insertNode(myList.createNode(d));
        }   
       
                     // myList.showList();
        
        System.out.println("Enter Some Names");
        BufferedReader searchInput=new BufferedReader(new InputStreamReader(System.in));
        String searchString=searchInput.readLine();
        
       
        
        System.out.println(myList.searchNode(searchString));
        
        
        
}
}

Thanks

Shahab

Your problem is in this line of code:

if(temp.getData().getData()==str)

When comparing Strings you cannot use == to determine if they have the same contents. You must use the equals() method of the String class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.