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

Thread: Reversing lines using LinkedList

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reversing lines using LinkedList

    Hi, I'm writing a program that reverses the lines of a String using the LinkedList structure, which includes an iterator. I have my code written out, but every time my program runs, it takes a very long time before it outputs the lines. I'm having difficulty proceeding to the next step. If anyone could point me to the right direction on what to do next, I would greatly appreciate it.

    Also note: I'm unable to call the reverse() method from the Collections class.

    import java.util.*;
    import java.util.ListIterator;
    import java.io.*;
     
    public class ReverseList
    {
       public static void main (String[] args)
       {
     
            LinkedList<String> phrase = new LinkedList<String>();
            phrase.add("Four");
            phrase.add("score");
            phrase.add("and");
            phrase.add("seven");
            phrase.add("years");
            phrase.add("ago");        
            System.out.println("Phrase in normal order: " + phrase);
     
            reverse(phrase);
     
            System.out.println("Phrase in reverse order: " + phrase);
        }
     
        public static LinkedList<String> reverse(LinkedList<String> a)
        {
          ListIterator<String> order = a.listIterator(a.size());
          LinkedList<String> reversed = new LinkedList<String>();
          while(order.hasPrevious())
          {              
             reversed.add(order.previous());
          }
          return reversed;
       }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reversing lines using LinkedList

    I don't understand your problem. On my computer your program runs normally and fast. However it doesn't reverse the list because the correct is phrase = reverse(phrase);

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reversing lines using LinkedList

    Yes, I too was able to receive the output I expected by modifying it as:

    System.out.println("Phrase in reversed order: " + reverse(phrase));

    Would I be able to use the reverse method to reverse the words of a file? For instance, if I called a text file and used the reverse method, would it be able to reverse the words of said file?

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reversing lines using LinkedList

    You can get the text file using the BufferedReader and store it in a variable. So, you can to split the variable using a function split("[ ]+").

    For example,

     
        // load text using BufferedReader
     
        String words[] = text.split("[ ]+");
     
        for(String word: words)
        {
             phrase.add(word);
        }
     
      phrase = reverser(phrase)
     
       // print phrase

    understand?

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reversing lines using LinkedList

    Not quite, this is what I have so far.

    	   Scanner scan = new Scanner (System.in);
    	   File f = new File("prezodent.txt");	
    	  System.out.print("Enter file name: ");
    	  Scanner input = new Scanner (new File(scan.nextLine()));
    	  scan.useDelimiter(" ");
    	  System.out.println();
    	  System.out.println(input);

    But it gives me this long stretch of a runtime error every time I enter the file name.

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reversing lines using LinkedList

    You are printing the object "input" and not its content. You can print the contents of the file this way:

     
     Scanner scan = new Scanner (System.in);
    	   File f = new File("prezodent.txt");	
    	  System.out.print("Enter file name: ");
    	  Scanner input = new Scanner (new File(scan.nextLine()));
    	  scan.useDelimiter(" ");
    	  System.out.println();
    	  while(input.hasNextLine())
    	  {
    		  System.out.println(input.nextLine());
    	  }

    helped?

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reversing lines using LinkedList

    Yes. I have one last question. How can I take the text file/input and convert it to a linkedlist, so it can use the reverse method?

    LinkedList<String> list = new LinkedList<String>();
    while(input.hasNextLine())
    {  
       System.out.println(input.nextLine());
    }
    System.out.println("\nWords in normal order: " + list);
    System.out.println("Words in reverse order: " + list);

  8. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reversing lines using LinkedList

    You can divide the lines of file in words so you add the words into linkedlist.

    For example:

     
               while(input.hasNextLine())
     	   {
        	         String[] words = input.nextLine().split(" ");
     
        	          for(String word: words)
        	         {
        		       phrase.add(word);
        	         }
     	   }
     
            System.out.println("Phrase in normal order: " + phrase);
     
            System.out.println("Phrase in reverse order: " +  reverse(phrase));

    This line is the important

     String[] words = input.nextLine().split(" ");

    It get one line of file and split the words using space how separator.

    understand?

Similar Threads

  1. How would you get a JTextArea to know how many lines it had?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2011, 07:58 PM
  2. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  3. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  4. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM
  5. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM