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

Thread: Implementing LinkedList as a user?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Implementing LinkedList as a user?

    Hello JPF! I have just been introduced to LinkedList and I am working on my first programming assignment, but I am really confused and lost. My professor wants us to implement the program as a user; I am unsure what she means, so can anyone please explain this? Because I have just been introduced to LL, I am just playing around with it, and so far, I haven't been able to get anything to work.

    I'm not writing any particular program at the moment, but I haven't been able to get the code below to print out right; I've been getting an error because there's something wrong with my insert method, but I do not know what. Can someone please tell me what I'm doing wrong?

    import java.util.LinkedList;
     
    public class Editor
    {
     
       private LinkedList<String> text;
     
       public String display()
       {
          return text.getFirst() + "\n" + text.getLast();
       }
     
     
       public void insert(String s)
       {
          text.add(s);
       }
     
    }

    public class Tester
    { 
       public static void main (String [] args)
      {  
          Editor e = new Editor();
          e.insert("test1");
          e.insert("test2");
          e.insert("test3");
          System.out.println(e.display());
      }
     
    }
    Last edited by vluong; October 15th, 2009 at 03:00 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Implementing LinkedList as a user?

    Hello,

    First thing I noticed when I tried to run your code was a nullpointerexception on the variable text.

    You need to actually initialise/create the linked list.

    private LinkedList<String> text = new LinkedList<String>();

    After that, everything worked just fine for me. For more information on the LinkedList have a look at LinkedList (Java Platform SE 6)

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    vluong (October 15th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Implementing LinkedList as a user?

    Thank you, Json!

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Implementing LinkedList as a user?

    If you are satisfied with the answer, please use the Thread Tools menu at the top to mark this thread as SOLVED.

    Cheers!

    // Json

Similar Threads

  1. Replies: 6
    Last Post: October 20th, 2009, 07:33 AM
  2. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 11:08 AM
  3. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 10:32 AM
  4. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 11:13 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 05:38 PM