1

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();
        }
}
Michelle
  • 61
  • 1
  • 2
  • 6

2 Answers2

0
LinkedList<String> dictionary = new LinkedList<String>(); 
System.out.println( dictionary ); //print it out

You need to write a function for LinkedList that will print the contents of the LinkedList in the way you want.

Right now it will only print the memory address of dictionary.

You will need to loop through the list and pick up the String stored in every Node and print it.

Something like this:

public void printList() {
    Node<T> node = getFirst();
    do {
        // get the String print it and move to the next node
    } while (node.myEnd != null);
}

EDIT: or use what @9000 wrote for printing.

Code Whisperer
  • 1,041
  • 8
  • 16
  • I have tried my print statement with other objects and it prints fine though – Michelle Apr 17 '15 at 15:57
  • What does it print. Does it print something readable or a bunch of characters. – Code Whisperer Apr 17 '15 at 15:58
  • Well I tried my print method with another testDriver and I did theList.add(20); theList.add(14); and it printed the integers. But with this, it prints: LinkedList@42a57993 for every word in the text file. I am not sure if it has to do with the object type – Michelle Apr 17 '15 at 16:00
  • Printing an object itself will always return the address, not a property of an object. `LinkedList@42a57993` tells you its a Linked List with the address `42a57993` as far as Java is concerned it is doing its job. `System.out.println(box)` will be memory address akin to what you are getting now, `System.out.println(box.color)` will be whatever is stored in the `color` property of `box`. – Code Whisperer Apr 17 '15 at 16:02
  • Search SO for more info, this is a common question. One example: http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Code Whisperer Apr 17 '15 at 16:04
  • Thank You! Checking it out now. I just thought that maybe it had to do with my testDriver because I felt like I was adding it twice. Once in the loadData method and again in the TestDriver by calling 'theList.add(...);' – Michelle Apr 17 '15 at 16:15
0

I suggest you take a top-down approach, and also use standard classes. (Reinventing a wheel may be fun, but sometimes you're better off with battle-proven wheels from java.util.)

Consider:

public LinkesList<String> readLines(BufferedReader source) {
   LinkedList<String> result = new LinkedList<>();
   while (true) {
      String line = source.readLine();
      if (line == null) break;  // end of file
      result.add(line);
   }  
   return result;
}

LinkedList<String> loadFile(Sting filename) {
   BufferedReader reader = null; 
   try {
     reader = new BufferedReader(new FileReader(filename));
     return readLines(reader);
   } finally {
     // free up the file, exception or not.
     // yes, this happens before the return, too.
     if (reader != null) reader.close(); 
   }
}

Printing a list (and, in fact, any iterable) is also dead easy:

List<String> lines = ...;
for (String item : lines) {
  System.out.println(item);
}
9000
  • 39,899
  • 9
  • 66
  • 104