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 5 of 5

Thread: Reversing an Array

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reversing an Array

        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();
            wordReverse(original);
            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
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing an Array

    cross-post: reversing-array

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reversing an Array

    From two different forums? I could see re-posting over and over on one forum but I went to a completely different forum. Instead of receiving help I have you tracking me down on different forums to tell me how I am wrong.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reversing an Array


  5. #5
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Reversing an Array

    The problem is that you are reversing the words' character order using the same Stack. So, this is what is happening:
    hello world is entered
    You take hello and put it on a stack in reverse order
    The stack looks like this -> olleh
    You then take world and put it on the same stack in reverse order
    The stack now looks like this -> dlrow olleh
    Then you pop the stack and add each element to a stringbuilder
    The string builder adds d into stringbuilder index 0 -> d
    The stringbuilder adds l into stringbuilder index 1 -> dl
    The stringbuilder add r into stringbuilder index 2 -> dlr
    Etc, until you get -> dlrow olleh
    Even in the fixOrder() method, you are using the Collections reverse() method which just does the same thing that you did in the wordReverse() method.
    You would really need to do something like putting each word into it's own variable, reversing the character order, then add the two words back together.

Similar Threads

  1. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  2. Do Loop, Reversing a Number Example, Incorrect Code in Book?
    By djl1990 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 24th, 2011, 07:01 AM
  3. reversing text in a file then putting it to a new file
    By derekxec in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2011, 09:36 PM
  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, 06:10 AM