Results 1 to 2 of 2
Thread: Reversing an Array
- 07-07-2012, 11:38 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
Reversing an Array
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?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; } }
-
Re: Reversing an Array
Cross-posted: reversing-array
Last edited by Fubarable; 07-08-2012 at 12:28 AM.
Similar Threads
-
Reversing an array
By severus1 in forum New To JavaReplies: 12Last Post: 06-30-2011, 05:34 PM -
Array reverse algorithim - reversing elements
By Adomini in forum New To JavaReplies: 9Last Post: 08-30-2010, 04:13 AM -
Reversing the String
By Inaam in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:35 PM -
Reversing
By whosadork in forum New To JavaReplies: 14Last Post: 11-06-2008, 04:29 AM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks