Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: create linked list from file?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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++;
    }


    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: create linked list from file?

    Thank you so much. I think i get it. It's working now

Similar Threads

  1. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  2. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  3. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM
  4. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM