3

so I have this linkedlist that is full of names. The user would search the first letter of a name and it would print out the nodes that the names start with the letters. When I run it thought I am not getting any response back. Althought if I insert some print lines in the loops I am getting those back.

Here it is:

public String printSection(){
        LinkedListNode current = front;
        String searchedLetter;
        int i = 0;
        String retSec = "The nodes in the list are:\n";

        //Get the input of the name being removed
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the first letter of the section you would like to print out");
        searchedLetter = s.nextLine();

        //while current is not null
        while(current != null){
            //if the data in current starts with the letter entered for searchedLetter
            if(current.getData().startsWith(searchedLetter)){
            //if(current.getData().substring(0,1) == searchedLetter){
                        //Increment the number of the node
                        i++;
                        //Print the node(s)
                        retSec += "Node " + i + " is: " + current.getData() + "\n";
                        //Traverse the list
                        current = current.getNext();
                        System.out.println("You made it here");
            }
        }
        return retSec;
    }
}

Here it is: (The new working method)

public void printSection(){

    LinkedListNode current = front;
    String searchedLetter;
    int i = 0;

    //Get the input of the name being removed
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the first letter of the section you would like to print out");
    searchedLetter = s.nextLine();

    //while current is not null
    while(current != null){
        //if the data in current starts with the letter entered for searchedLetter
        if(current.getData().startsWith(searchedLetter)){
                    //Increment the number of the node
                    i++;
                    //Print the node
                    System.out.println("Node " + i + " is: " + current.getData());
        }
        //Traverse the list
        current = current.getNext();
    }
}
Shawn
  • 2,355
  • 14
  • 48
  • 98
  • 1
    The issue might be your declaration of current to `front`, so first make sure that's all good. Then, look and see if that 2nd while loop is actually necessary. It seems like you only need the first one to check if you haven't reached the end of the linked-list. – Marcel Puyat Nov 07 '13 at 08:12
  • You could convert your list to a Set and then use the subset method. Check out this answer: http://stackoverflow.com/questions/7228350/searching-for-a-record-in-a-treeset-on-the-fly – W.K.S Nov 07 '13 at 08:18

1 Answers1

2

You are running into an endless loop here.

You only need one while loop and you have to jump to the next element in your list no matter if the element starts with the searched letter or not.

So rework your if statement and remove the 2nd while loop. Also make sure to always go to the next element.

Edit: Looking a bit deeper at your code I realized you also do not check the input you ask from the user. He is actually not restricted to a single character, but can enter a whole line of text. So either fix the explanation you give him, or introduce a validation of your input (including some error message if the input is invalid).

Matthias
  • 3,582
  • 2
  • 30
  • 41
  • Very well explained, I got it figured out. Thanks for answering it is much appreciated and will be marked as answer when timer is up. I am going to add the restrictions to the input as well as what to do if a node doesn't start with the letter they give. Thanks once again ;) Also updated the code to show new method – Shawn Nov 07 '13 at 08:20
  • Glad I could be of help. Good luck for your learning. It would be good however if you keep the original code and add the solution seperately so that other users with similar problems can learn from this. – Matthias Nov 07 '13 at 08:23