0

I'm trying to read a file (in this case a text file with song lyrics) and create a linked list using only unique strings from said file. There cannot be any two same strings in the list. It needs to be stored in a Linked List and can't use the built-in one for it. Right now this is what I have:

public String createSuperList(String newKey) {
    SuperLink newSuperLink = new SuperLink(newKey);
    SuperLink current = first;
    if(isEmpty()) {
        incertLast(newKey);
    } else if (newSuperLink != last) {
        while(current != newSuperLink && current != null){
            if(current.equals(newSuperLink)){
                return null;
            } else {
                incertLast(newKey);
            }
            current = current.next;
        }
    } else {
        return null;
    }
    return newKey;
}

HOW THE NEWKEY IS SENT TO THIS METHOD:

    File file = new File("MYFILEPATH");
    try {
        Scanner sc = new Scanner(new FileInputStream(file));
        while (sc.hasNextLine()) {
            content = sc.next();
            if(SuperList.createSuperList(content) == null){
                BabyList.incertLast(content);
            }
        }
        sc.close();
    } catch (FileNotFoundException fnf) {
        fnf.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("\nProgram terminated Safely...");
    }

The newKey is the string that I'm comparing to the rest of the Linked List to see if it is repeated anywhere in the list and, if it is seen in the List somewhere, it will return a null.

My issue right now is that it just keeps going and there's no stopping (runaway), it doesn't end at all. The program doesn't stop. I keeps looping forever. I had it running for an hour with nothing changing and it still running.

Right now what its suposed to do Is create a LinkedList called superLinkList that only has unique words in it.

This is going to be a markov text generator that uses 2 linked lists.

EDIT_1: So I fixed the infianite run time issue but Its still not checking if its unique or not.

Updated Code:

public Boolean createSuperList(String newKey) {
    SuperLink newSuperLink = new SuperLink(newKey);
    SuperLink current = first;
    boolean i;
    if(isEmpty()){
        incertLast(newSuperLink.toString());
    }       
    while(current != newSuperLink && current != null){
        if(current.equals(newSuperLink)){
            System.out.println("Not unique Item");
            i = false;
        } else {
            System.out.println("Unique Item");
            i = true;
        }
        current = current.next;
    } 

    if(i = true){
        return true;
    }else{
        return false;
    }

}

EDIT_2:

Okay so now its a nullpointerexception. The NPE is at the while loop. Specifically its current.keyWord. Im not sure how to change this so that It will work. Im not sure why its throwing an error here....

Code:

public Boolean createSuperList(String newKey) {
    SuperLink newSuperLink2 = new SuperLink(newKey);
    SuperLink current = first;
    boolean i = false;
    if (isEmpty()) {
        incertLast(newKey);
    }
    while (!current.keyWord.equals(newSuperLink2.keyWord) && current != null) {
        if (current.keyWord.equals(newSuperLink2.keyWord)) {
            System.out.println("Not unique Item");
            i = false;
            break;
        } else {
            System.out.println("Unique Item");
            i = true;
        }
        current = current.next;
    }
    if (i = true) {
        return i;
    } else {
        return i;
    }

}
  • 2
    This code snippet doesn't really demonstrate your problem. Consider creating an [MCVE](https://stackoverflow.com/help/mcve) to show what exactly happens. P.S. you shouldn't [compare strings with `==`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – default locale Oct 19 '16 at 18:53
  • I edited the question to make it a bit easier to read and hopefully more specific. – Shawn Williams Oct 19 '16 at 19:02
  • Your checking duplicate & insertion logic seems really weird and completely buggy. Have you tried to debug it with a simple instance problem to see where it loops? – Patrice Gahide Oct 19 '16 at 19:07
  • How would you suggest that I go about doing that? Im testing that right now, ill update the original question when I get an result. – Shawn Williams Oct 19 '16 at 19:16
  • I updated the question. I found my looping error and changed my code to reflect that. Its still not checking for unique words. It says they all are even when they are not – Shawn Williams Oct 19 '16 at 21:04
  • > "There cannot be any two same strings in the list" Then list is incorrect data structure - you need a **set**. – David Soroko Oct 28 '16 at 10:48

1 Answers1

0

You can solve your problem in a few lines:

  • read values into a LinkedHashSet (set preserving the order of insertion)
  • convert the result new LinkedList<>(linkedHashSet)
Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62