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 2 of 2

Thread: Creating a Linked List from the contents of a file

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a Linked List from the contents of a file

    Hi, I'm working on an assignment for programming class where we're supposed to create a method that takes the contents from a text file (one giant string) and stores them piece by piece in a linked list. I'm a little stuck and not sure where to go from here, or if I'm on the right track...

    import java.util.*;
    import java.io.File;
    import java.io.FileNotFoundException;
     
    public class LinkedLists
    {
        // instance variables - not sure if I should use these?
        // private String file;
        // private LinkedList<String> list;
     
        /**
         * A method to read the items from a file and convert them to a list.
         * @param fileName the file to be read
         * @return readItems a linked list containing read items.
         */
        public static LinkedList<String> readItems(String fileName) throws FileNotFoundException
        {
            File input = new File(fileName);
            Scanner in = new Scanner(input);
            String file = "";
     
            // Copy the contents from the file to a String
            while (in.hasNext())
            {
                file = in.next();
            }
     
            // Create the list
            LinkedList<String> list = new LinkedList<String>();       
            ListIterator<String> iter = list.listIterator();
     
            // Copy the String contents to the Linked List
            while (iter.hasNext())
            {
                for (int i=0; i<file.length(); i++)
                {
                    file = in.next();
                    list.add(file);
                }
            }
     
            return list;
        }
    }

    There is also a tester class which is supposed to create a linked list from the text file and print the list.

    import java.io.File;
    import java.util.Scanner;
     
    public class LinkedListTest
    {
        public static void main(String[] args)
        {
            File input1 = new File("input1.txt");
            Scanner in = new Scanner(input1);
     
            String s = in.next();
     
            System.out.print(readItems(s));
        }
    }

    I'm not sure how to implement my readItems method into this main method. Should I convert my text file to a String in the main method too? So confused...

    *** I've also posted here for help. ***


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Linked List from the contents of a file

    Separate the problem of the reading of a String from the creating of a linked list.

    The s variable in the posted code has the String from the file. Is there any more to read from the file?

    Now work on separating the String into parts and putting the parts into the linked list.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  4. create linked list from file?
    By junsugal in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 13th, 2011, 12:39 AM
  5. 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