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: Java Recursion reversing my strings

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Recursion reversing my strings

     
    import java.util.ArrayList;
     
    import java.util.Scanner;
     
     
     
    public class ReverseWords {
     
         static Scanner Scan = new Scanner (System.in);
     
     
     
        public static void main(String args[]) {
     
            System.out.println("Enter a list of words, one per line.  Final word should be a period (.)");
     
            String words = Scan.nextLine();
     
            reverseRecursively (words);
     
        }
     
     
     
     
     
        public static void reverseRecursively(String words)
     
        {
     
            int i;
     
            ArrayList<String> WordLists = new ArrayList<String>();
     
            for ( i = 0; i < WordLists.toArray().length; i++)
     
                WordLists.add(words);
     
            if (words.equals("."))
     
            {
     
     
     
                    System.out.println(WordLists.toArray()[i]);
     
                System.out.println("Period");
     
            }
     
            else
     
            {
     
                System.out.println("Enter New Word");
     
                    words = Scan.nextLine();
     
                WordLists.add(words);
     
                reverseRecursively(words);
     
            }
     
        }
     
    }

    Please help me. Why isn't my code working?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Java Recursion reversing my strings

    You tell us. What does the code do exactly? What happened when you stepped through it with a debugger, or at least added some print statements, to determine exactly where the program's execution differs from what you expect?
    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
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Recursion reversing my strings

    The mode is supposed to take one word at a time from the user, for example, "Tim" "Likes" "Dogs" ".". When the period is entered it will end the recursion and return "Dogs" "Like" "Time". When I added Print statements it was recognizing the period and when to continue. It just refuses to output the actual words.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Java Recursion reversing my strings

    Step through your code one line at a time.

    You get a single String which presumably holds multiple words and a period.
    You pass that String into your reverseRecursively method.
    You create a new ArrayList, which is empty.
    You iterate over that ArrayList, which again is empty, and add each item (there are no items) to the ArrayList again for some reason.
    You then check whether the whole String (which is a full sentence) is a period or not.
    If it is you print out the word at the i index. But since your ArrayList is empty, i will always be zero.
    If it's not, you ask for a new sentence and repeat the process.

    Why did you expect this code to work?
    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
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Recursion reversing my strings

    been up for 24+ hours. I have been literally trying everything and this was the closet I came to actually getting input.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Java Recursion reversing my strings

    Sounds like it's time for a good night's sleep. Then take a step back and really think about what you need to do, one small step at a time. Recommended reading: How to Program (stuck on homework? READ THIS BEFORE POSTING)
    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
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Recursion reversing my strings

    Thanks mate. Been doing this for the better part of 7 hours. Just getting annoyed/desperate haha. Thanks again.

Similar Threads

  1. [SOLVED] Java recursion
    By maple1100 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2013, 12:54 PM
  2. Reversing an Array
    By georger55 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2012, 07:22 AM
  3. Need help with Java recursion problem!
    By eyesackery in forum Java Theory & Questions
    Replies: 2
    Last Post: June 5th, 2012, 08:12 AM
  4. Reversing an array
    By severus1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 30th, 2011, 03:54 PM
  5. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 05:10 AM