M
mmoski
Here is some code. It reads in a line, then puts each token in that
line into a list node. The problem is that it seems that the next
token simply gets put into the data field of the head node. This is
causing the .next to NEVER equal null and crashes JCreator. This list
is implemented myself because this is my first time using a linked
list and I don't want to use any pre-defined list classes. Any
suggestions as to why the head node won't stay as the head node?
import java.util.Scanner; //import everything
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Parse {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader( new
InputStreamReader(System.in));
Node head = null; // set up head node
Node prev = null; // set up a node to keep track of position
Node a = new Node(); //set up node that will hold data
String line = null;
String word = null;
while(((line = br.readLine()) != null) && (!line.equals(""))) { //
reads in a line
Scanner sc = new Scanner(line);
while(sc.hasNext()){ //scans line for tokens
word = sc.next();
a.data = word; //sets data to the next word
if(head == null){ //if there is no head create one and make
it's data field equal a
head = a;
}else{ //if there is a just a head and 0 or more other nodes
make the current node's next field point to a
prev.next = a;
}
prev = a; // set the 'keeping track of' node to equal a.
}
}
Node pointer = head;
while(pointer != null){ //test to see if the pointer node exists
System.out.println("Working " + pointer.data); //print out the
data of the pointer node
pointer = pointer.next;//set the pointer to the next node
}
}
}
class Node{
public Node next = null;
public String data;
}
Any help would be freakin' awesome. Thanks.
line into a list node. The problem is that it seems that the next
token simply gets put into the data field of the head node. This is
causing the .next to NEVER equal null and crashes JCreator. This list
is implemented myself because this is my first time using a linked
list and I don't want to use any pre-defined list classes. Any
suggestions as to why the head node won't stay as the head node?
import java.util.Scanner; //import everything
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Parse {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader( new
InputStreamReader(System.in));
Node head = null; // set up head node
Node prev = null; // set up a node to keep track of position
Node a = new Node(); //set up node that will hold data
String line = null;
String word = null;
while(((line = br.readLine()) != null) && (!line.equals(""))) { //
reads in a line
Scanner sc = new Scanner(line);
while(sc.hasNext()){ //scans line for tokens
word = sc.next();
a.data = word; //sets data to the next word
if(head == null){ //if there is no head create one and make
it's data field equal a
head = a;
}else{ //if there is a just a head and 0 or more other nodes
make the current node's next field point to a
prev.next = a;
}
prev = a; // set the 'keeping track of' node to equal a.
}
}
Node pointer = head;
while(pointer != null){ //test to see if the pointer node exists
System.out.println("Working " + pointer.data); //print out the
data of the pointer node
pointer = pointer.next;//set the pointer to the next node
}
}
}
class Node{
public Node next = null;
public String data;
}
Any help would be freakin' awesome. Thanks.