1 Replies - 1954 Views - Last Post: 26 September 2013 - 07:47 AM Rate Topic: -----

#1 Naked Snake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-December 12

Trying to reverse a string in an arraylist with recursion

Posted 26 September 2013 - 07:31 AM

Hey, I am trying to get the user to input words, one at a time, and then input a period to signify they are done inputting values. The problem is my code knows when to stop and when to continue but it doesn't give me any values. It will give me Print out lines telling me when it follows the recursion cycle but none of the inputs

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 try to lead me in the right direction. i have been at this for a while and I am really frustrated. It was to do something with my logic I just don't know what.

This post has been edited by jon.kiparsky: 26 September 2013 - 07:39 AM
Reason for edit:: removed gratuitous whitespace


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to reverse a string in an arraylist with recursion

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Trying to reverse a string in an arraylist with recursion

Posted 26 September 2013 - 07:47 AM

Start by isolating the functionality. Currently your reverseRecursively method is getting user input, which it shouldn't do. Clean it up so that it does nothing but reverse and it'll be easier for you to see where to go next.


The structure of the program should be something like

wordList = getWordList();
reversedList = reverse(wordList);
// from here you can output the wordlist or send it to a file or post it on the web or whatever you want to do


As for the structure of your reverse method, it's probably easiest if you practice this on paper first. Find the base case and then find the recursive step. Arrange things so the recursive step always simplifies towards the base case.

As a final hint, I would not want to have the "." terminator stored in the actual array or list that you're reversing. That is only a signal to the input process that it's time to stop accepting input. Once it's done that, it's not needed any more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1