Molly
Posts: 1
Nickname: seksee
Registered: May, 2004
Reversing Order of Words in a Sentence
Posted: May 14, 2004 4:21 PM
Advertisement
I really need some help on this, I can't get my program to run right. I need to write a java program to reverse the order of words of an input sentence until the user says "no more" I have this so far: import java.io.*; import java.util.*; public class ReverseWords { public static void main(String args[]) throws IOException { BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in)); boolean anotherSentence=true; { while(anotherSentence) { System.out.print("Type in any sentence without punctuation: "); Stack stack=new Stack(); StringTokenizer tokenizer=new StringTokenizer(keyboard.readLine()); while(tokenizer.hasMoreTokens()) { stack.push(tokenizer.nextElement()); } System.out.print("Reverse word string: "); while(!stack.empty()) { System.out.print(stack.pop()); System.out.print(" "); } System.out.println(" "); if(tokenizer.equals("no more")) anotherSentence=false; } } } } Everything works right except if I type "no more" in the input area, it just keeps running; doesn't stop. Can anyone help me out??