I am trying to write a program that saves the content of a text file into a singly linked list, every line in the txt file is a node. I used Scanner to read from the file but I am having trouble placing the Text into a linked list.
I created a linked list class and a Node class.
Any hint or help is would be helpful.
This is my Linked list class:
import java.util.Iterator;
public class LinkedList<T> { //Generic
private Node<T> myStart;
private Node<T> myEnd;
private int mySize;
public LinkedList(){
mySize = 0;
myStart = null;
myEnd = null;
}
// Clears the List
public void clear(){
myStart = null;
myEnd = null;
mySize = 0;
}
public T getFirst(){
if (myStart == null){
return null;
}
return myStart.getValue();
}
public boolean isEmpty() {
// Returns ONLY and ONLY IF "myElements" equals to 0
return mySize == 0;
}
public int size() {
return mySize;
}
//Add a value to the list
public void add(T aValue ){
Node<T> theNode = new Node<T>(aValue);
theNode.setLink(myStart);
myStart = theNode;
myEnd = theNode;
mySize++;
}
//Adds a value to the end of the List
public void addToEnd(T aValue){
Node<T> theNode = new Node<T>(aValue);
if(myEnd == null){
myStart = theNode;
myEnd = theNode;
}
else {
myEnd.setLink(theNode);
myEnd = theNode;
}
mySize++;
}
//Removes a value from the list
public void remove(T aValue){
Node<T> current = myStart;
Node<T> previous = null;
while(current !=null){
if(current.getValue().equals(aValue)){
mySize--;
if (previous == null){
myStart = current.getLink();
}
else{
previous.setLink( current.getLink() );
}
}
if(current == myEnd){
myEnd = previous;
}
previous = current;
current= current.getLink();
}
return;
}
//Prints the current list
public void print(){
Node<T> current = myStart;
while(current !=null){
System.out.print(current.toString() + " ");
current= current.getLink();
}
System.out.println();
}
}
Then I tried to read in the file which I could but I don't know why its not printing the List correctly. Here is where I added the file into the linked list:
public class SpellCheck<T> {
public SpellCheck(){
}
//LoadData reads the file and places it in a linkedList
public static String loadData(String fileName){
LinkedList<String> dictionary = new LinkedList<String>(); //Create a new LinkedList called dictionary
Scanner scan = null;
try {
scan = new Scanner(new FileInputStream(fileName)); //read in the file
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
while(scan.hasNext()) {
String theList = scan.nextLine();
dictionary.add(theList); //Add theList to the LinkedList "Dictionary"
System.out.println( dictionary ); //print it out
}
scan.close();
}
}
My text file has random words such as: aback abaft abandon abandoned abandoning abandonment basketballs baskets basking Basque bass
In my Test Driver this is what I did in my TestDriver class: public class TestDriver {
public static void main(String[] args) {
LinkedList<String> theList = new LinkedList<String>();
theList.add(SpellCheck.loadDataLinkedList("standard.txt"));
theList.print();
}
}