Results 1 to 2 of 2
  1. #1
    georger55 is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Reversing an Array

    Java Code:
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to the word reversing program");
            System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
            System.out.print("Please enter a Sentence: ");
            String original = input.nextLine();
            String reverse = wordReverse(original);
            List<String> nSentence = fixOrder(reverse);
            System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + nSentence );
        }
        
        public static String wordReverse(String original){
    
            StringTokenizer string = new StringTokenizer(original);
            
            Stack<Character> charStack = new Stack<Character>();
    
            while (string.hasMoreTokens()){
    
            String stack = string.nextToken();
    
            for (int i = 0; i < stack.length(); i ++){
    
            charStack.push(stack.charAt(i));
        }
            charStack.push(' ');
        }
    
            StringBuilder result = new StringBuilder();
            while(!charStack.empty()){
            result.append(charStack.pop());
        }
    
            return result.toString();   
        }
     
        public static List<String> fixOrder(String reverse){
           
            List<String> list = Arrays.asList(reverse);
            Collections.reverse(list);
           
           
           return list;
        }
    }
    The code reads as everything is fine but the output is not reversed. If the input is "hello world" the output should be "olleh dlrow". The words are reversed to "dlrow olleh" in the word reverse method. I created a fixOrder method to reverse the order of the string so the output should be "olleh dlrow". Even with the reversal in the array i am still getting the output "dlorw olleh". Any suggestions?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Reversing an array
    By severus1 in forum New To Java
    Replies: 12
    Last Post: 06-30-2011, 06:34 PM
  2. Array reverse algorithim - reversing elements
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 08-30-2010, 05:13 AM
  3. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 09:35 PM
  4. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 05:29 AM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •