-2

Lines should be read from the user until the sentinel "DONE" is entered, at which point they are displayed in reverse. Do not print the word "DONE"

here is my code so far i am not sure how to get it all to work together i know i have been overthinking this but any help will be appreciated

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

public class Turner_A05Q3{
    public static void main(String[] args) {
        Stack<String> stack = new Stack<String>();
        ArrayList<String> list = new ArrayList<String>();
        System.out.println("Enter text, or done to stop");
        Scanner scanner = new Scanner(System.in);    
        List<String> al = new ArrayList<String>();   
        String word;                                  
        while (scanner.hasNextLine()) {               
            word = scanner.nextLine();                  
            if (word != null) {                         
                word = word.trim();                       
                if (word.equalsIgnoreCase("done")) {      
                    break;                                  
                }
                al.add(word);                             
            } else {
                break;                                   
            }
        }
        for (int i=0;i<word.length();i++){
            stack.push(word.substring(i,i+1));
        }
        String wordrev = "";
        while(!stack.isEmpty()){
            wordrev += stack.pop(); 
        }
        System.out.println("Reverse of the text \"" + wordrev);                      
    }
}
Ryan Jackman
  • 780
  • 2
  • 8
  • 24
zompire
  • 1
  • 1
  • Are the sentences supposed to be displayed in reverse order of entry, or is each line supposed to be reversed - e.g. "Some input" becomes "tupni emoS"? – Andrew Williamson Feb 12 '16 at 18:20
  • The formatting of this code makes is difficult to follow. Aside from that, what are you asking for? What does/doesn't work? – Whymarrh Feb 12 '16 at 18:20
  • https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#reverse() might be helpful –  Feb 12 '16 at 18:23

2 Answers2

0

Try to write your for loop like this:

  for (int i = word.length(); i > 0; i--) {
        stack.push(word.substring(i-1, i));
    }

Input:

DONE

Output:

 DONE
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

You are always working with the last word (done), because you don't use al you only use the last content of the variable word.

You have to iterate over al

Try:

import java.util.Scanner;
import java.util.Stack;
import java.util.ArrayList;
import java.util.List;

public class Turner_A05Q3{
     public static void main(String[] args) {
         Stack<String> stack = new Stack<String>();
         System.out.println("Enter text, or done to stop");

         Scanner scanner = new Scanner(System.in);    
         List<String> al = new ArrayList<String>();   
         String word=null;          

         while (scanner.hasNextLine()) {               
             word = scanner.nextLine();                  
             if (word != null) {                         
                word = word.trim();                       
                  if (word.equalsIgnoreCase("done")) {      
                      break;                                  
                  }
                al.add(word);                             
             }else {
                 break;                                   
             }
         }

         for(int j=0;j<al.size();j++){ 
             word = al.get(j);       
             for (int i=0;i<word.length();i++){
                 stack.push(word.substring(i,i+1));
             }

             String wordrev = "";
             while(!stack.isEmpty()){
                 wordrev += stack.pop(); 
             }
             System.out.println("Reverse of the text \"" + wordrev);                      
         }
    }
}

If you want to show the words in reversed order, not the characters, try:

public static void main(String[] args) {
     Stack<String> stack = new Stack<String>();
     System.out.println("Enter text, or done to stop");

     Scanner scanner = new Scanner(System.in);    
     String word=null;          

     while (scanner.hasNextLine()) {               
         word = scanner.nextLine();                  
         if (word != null) {                         
            word = word.trim();                       
              if (word.equalsIgnoreCase("done")) {      
                  break;                                  
              }
            stack.push(word);                              
         }else {
             break;                                   
         }
     }

     while(!stack.isEmpty()){
         System.out.println("Reverse of the text \"" + stack.pop()); 
     }
}

Remember that a Stack is LIFO (Last In First Out)

  • thank you so much just curious how would you change it to show the words reversed not the characters i am not understanding how to tell the program to do that – zompire Feb 12 '16 at 21:22