create linked list from file?
I have 2 problems here.
I am supposed to read from a file that contains the name of players and amount of golds.
So in each line in the file there is one string value and one int value.
My node should store the string value and int value in the same node.
I know there is a next and nextInt but it doesn't work either.
My another problem is storing the data into the node itself.
I think i create the nodes without connecting them.
Because when i tried to print them in my print method, it only print the last element and its only the amount of the gold.
private Node first;
private Node last;
private int size;
public GameManager(File startUpData) throws IOException{
Scanner player = new Scanner(startUpData);
while(player.hasNext() && player.hasNextInt()){
last = new Node(player.next(), player.nextInt(), null);
first = last;
first = first.next;
size++;
}
}
Re: create linked list from file?
What do you think your while statement is doing?
while(player.hasNext() && player.hasNextInt()){
If I'm understanding you correctly, your Scanner is going to be at a line that has a String followed by an integer. So won't hasNextInt() return false?
Re: create linked list from file?
So, how do i read 2 values in one line?
It seems to me that next and nextInt has no difference.
When i have a print statement in my while loop, and assign it to print the current item, it can prints me all the string and integer value.
But it seems that my node will be doubled. Because 1 node will be storing the string and the 2nd node will be storing the integer value.
Re: create linked list from file?
I'm not sure what you mean. If the next token is not an int, I don't see hasNextInt() returning true.
But the code inside your while loop doesn't make a ton of sense either. Each iteration, you're creating a new Node and storing it in last. Then you're also storing it in first, but then you're immediately making first equal to its next, which I'm guessing is null. Basically, you never keep track of the previous instances of Node.
Re: create linked list from file?
I am so sorry, I am new to java and scanner and linked list confused me.
So, what i meant is, when I only use hasNext() and use next to get the value, it can return my integer value too.
So how can i read one line that has 2 tokens (string and int) and get that 2 token separately and store it to my node?
because if I understand correctly, in nodes, we can just use node.next to get the 1st token and node.next.next to get the 2nd token.
How about scanner? Because i need to get the 1st and 2nd token and store it into one node.
My other problem is how to keep track of previous instance of node.
Is this the right way?
while(player.hasNext()){
// create new node and set it to last
last = new Node(....);
first.next = last;
first = first.next;
}
Re: create linked list from file?
Take a look at the Scanner API for a tutorial on how to parse things: Scanner (Java Platform SE 6)
Re: create linked list from file?
Thank you so much. I think i get it. It's working now