user3023346
user3023346

Reputation: 33

getNext() Linked List

I'm really new to Java and StackOverflow so please don't be mean. I would really appreciate some help. Thank you in advanced.

I feel like this is really easy and I've already tried this out a million different ways but it's not working.

I am trying to take in a text file and store it into a linked list and I'm trying to access the third node of this linked list. For some reason, I can access the first node, then I can use the getNext() command to go to the next node, but when I try to use getNext() to go the third node, it continues to return the second node. So it is not going to third node. Am I just missing some key concept? Also let me know if you need anymore information.

The text file that is being taken in is: 5 A B C D E A B //This is the line that I want B C B D C D C E D E

Here is part of my code:

public static void main(String[] args) throws IOException{
    /**
     * Check whether the user types the command correctly
     */
    if (args.length != 1)
    {

        System.out.println("Invalid input");
        System.out.println(args.length);
        System.exit(1);
    }

    String filename = args[0];
    Scanner input = new Scanner (new File(filename));

            LinkedList<String> linkedList= new LinkedList<String>();

            while(input.hasNext())
    {
        linkedList.addToRear(input.nextLine());
    }

            LinearNode<String> link= linkedList.firstLink;

            String temp = " ";
    link.getNext();
    temp = (String)link.getElement();
    String[] numofVerticesArray = temp.split(" ");
    int numOfVertices = Integer.parseInt(numofVerticesArray[0]);
    int lineNumber = 1;

    String [] arrayOfVertices; 
    LinearNode<String> secondLine = link;
    String temp2;


    for (int i=0; i <= lineNumber; i++)
    {
        secondLine = link.getNext();
    }
    lineNumber = 2;
    temp2 = (String)secondLine.getElement();
    arrayOfVertices = temp2.split(" ");

            int[][] adjMatrix = new int[numOfVertices][numOfVertices];

    LinearNode<String> edgeLine = link;
    String [] arrayOfEdge;
    int rowCount = 0;
    int columnCount = 0;
    String temp3;
    lineNumber = 2;

    for (int i=0; i <= lineNumber; i++)
    {
        edgeLine = link.getNext();
        System.out.print((String)edgeLine.getElement());
                    //When this is printed out, the second node's 
                    //content is printed out, not the third node
    }
    lineNumber++;
    temp3 = (String)edgeLine.getElement();
    arrayOfEdge = temp3.split(" ");

Upvotes: 3

Views: 30742

Answers (2)

Andrei Nicusan
Andrei Nicusan

Reputation: 4623

The only assignment you make to the link variable is this: link= linkedList.firstLink;. You're never assigning anything else to it. So calling link.getNext() will always return the same node, namely the second one. link is not an iterator, thus you can't call getNext() and advance through the linked list.

Upvotes: 0

Quillion
Quillion

Reputation: 6476

You keep on asking for the second element from the LinkedList.

edgeLine = link.getNext();

sets the value of second element of the LinkedList link into edgeLine, and then you loop and do the same, and then the same over and over and over again.

Try doing

edgeLine = edgeLine.getNext();

This will keep on moving.

Upvotes: 6

Related Questions