The Artima Developer Community
Sponsored Link

Java Answers Forum
Reversing Order of Words in a Sentence

1 reply on 1 page. Most recent reply: May 15, 2004 12:13 AM by Dhrubo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Molly

Posts: 1
Nickname: seksee
Registered: May, 2004

Reversing Order of Words in a Sentence Posted: May 14, 2004 4:21 PM
Reply to this message Reply
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??


Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: Reversing Order of Words in a Sentence Posted: May 15, 2004 12:13 AM
Reply to this message Reply
Hi darling ,
This one does what u want
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;
String lineread="";
//dont declare variables in a loop
Stack stack = null;
StringTokenizer tokenizer = null;

{
while(anotherSentence)
{
System.out.print("Type in any sentence without punctuation: ");
stack=new Stack();
lineread = keyboard.readLine();
//line added by Dhrubo
if(lineread.equals("no more"))
break;

tokenizer=new StringTokenizer(lineread);

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;
}
}

//line added by Dhrubo
//always close the stream best practise
keyboard.close();
}
}

---dhrubo
dhrubo@indiainfo.com

Flat View: This topic has 1 reply on 1 page
Topic: need help for table stuff Previous Topic   Next Topic Topic: java beans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use