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;
}
}