Results 1 to 3 of 3
  1. #1
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

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

    Java Code:
    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.

    Java Code:
    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...

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

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

    Are you certain the instructor wanted you to use the JDK version of the LinkedList and not grow your own? Have you discussed linked lists in your class? If it is an exercise in linked list mechanics, using the built-in version won't teach you much.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

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

    Hey Jim, yeah that's his intent. (to use the built-in methods)
    Last edited by greystreet34; 05-07-2013 at 02:44 AM.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 05:07 AM
  2. Creating a node for polynomial linked list
    By Java-Guy in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 09:27 PM
  3. Replies: 4
    Last Post: 02-21-2011, 10:34 AM
  4. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 08:16 PM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •