0

I am supposed to reverse the individual words in a sentence using a helper method that takes a string as a parameter and returns a string. The stack is supposed to be in the helper method. So my program works in that it reverses the words correctly. But reverse isnt actually getting returned, i think its just printing the stack. Can anyone help me return and print the string variable 'reverse'.

import java.util.Scanner;
import java.util.Stack;

public class ReverseStack 
{
    public static void main(String[] args)
    {
        String sentence;

        System.out.print("Enter a sentence: ");
        Scanner scan = new Scanner(System.in);

        sentence = scan.nextLine();

        System.out.println("Reversed:" + PrintStack(sentence));
    }

    private static String PrintStack(String sentence)
    {
        String reverse = "";
        String next = "";

        Stack<String> stack= new Stack<String>();

        String words[] = sentence.split(" ");

        for(int j = 1; j<words.length +1; j++)
        {
            String newWord = words[words.length - j]; // Single word

             for(int i = 0; i < newWord.length(); i++)
            {
                    next = newWord.substring(i,i+1);
                    stack.push(next);
            }
             stack.push(" ");
        }
        while(!stack.isEmpty())
        {
            reverse += stack.pop();
        }
        return reverse;
    }   
}
6
  • Strings are immutable. Commented Feb 13, 2014 at 14:53
  • Your program is returning and printing the string variable 'reverse'. Commented Feb 13, 2014 at 14:58
  • @Simon But isnt it printing out the stack not the reverse string? Its printing PrintStack(sentence) not reverse Commented Feb 13, 2014 at 15:35
  • @user3071909 Well, we can say that you are printing both: 'PrintStack(sentence)' since you are calling it inside the Sysout and 'reverse' since it is the return value of 'PrintStack' method. Then, I don't understand what seems wrong to you. Commented Feb 13, 2014 at 15:42
  • @Simon oohhh so it is returning and printing reverse. I was just confused. Is the use of the stack correct, in terms of reversing the words? Commented Feb 13, 2014 at 15:48

1 Answer 1

1

You are reversing twice and ending up with the same order. Your Stack gives reverse order, but you are adding the words in reverse order, so the order is unchanged.

If you used a debugger it should be obvious to you what the issues is.

BTW You can make the code much shorter.

private static String printStack(String sentence) {
    Stack<String> stack= new Stack<String>();
    for(String word: sentence.split(" ")
        stack.push(word);
    String line = stack.pop();
    while(!stack.isEmpty())
        line += " " + stack.pop();
    return line;
}   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.