Returning a word backwards..

hi,

i want to write a program that reads in a word say "hello computer" adds it to a stack and returns it as "retupmoc olleh" So far all i have done is that it adds the word to a stack and returns it as "computer hello". I also have used a string tokenizer in the program as well.


my source is available at:

http://www.newwavesound.co.uk/java


pete
pete420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
StringBuffer sb = new StringBuffer("hello computer");
String reversed = sb.reverse();
0
pete420Author Commented:
cheers for reply but do u know of any way to do it by using a stack to reverse the characters of each word?

pete
0
CEHJCommented:
Just giving you another option. Your code looks ok on first glance. What's the problem?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

pete420Author Commented:
thanks,

the problem with mine is that it just returns the words still spelt the correct way, ie it returns it the way a stack does in that last one put on teh stack is the 1st one off. ie when i enter "hello computer" i get back "computer hello".
know what i mean?

i want the words to be spelt backwards "olleh" for hello

ta
0
CEHJCommented:
You probably need to combine the two ;-)

0
CEHJCommented:
If you don't combine the two, you'll have to use the stack on a Character basis
0
pete420Author Commented:
combine the two what?

how do  i use stack on a character basis?

ta
0
CEHJCommented:
... which would be something like:

word = tokenizer.nextToken();
for(int i = 0;i < word.length();i++) {
      wordStack.push(new Character(word.charAt(i)));  
}            
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
... and later:

StringBuffer sb = new StringBuffer(wordStack.size());
while (wordStack.isEmpty() == false) {
      Character c = (Character)wordStack.pop();
      sb.append(c.charValue());
}
0
CEHJCommented:
>>combine the two what?

The two approaches (stack + StringBuffer). If this is a classwork question, you may be better to use the stack throughout
0
pete420Author Commented:
i appreciate this help because after coding the amount i have i feel i am near a solution,
it is a classword excercise,
how do i use a stack throughout? do i take out the line
sb.append (c.char.Value());

i have amended my code to this but its still not working:

my code is now this:

public class wordReverse
{

   public static void main (String[] args) throws IOException
   {
      int wordCount = 0, characterCount = 0;
      String sentence, word;
      StringTokenizer tokenizer;
         
      BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in));
     
      //creates a new stack
      LinkedStack wordStack = new LinkedStack();
     
      //requests info from user
      System.out.println ("Please enter a sentence (type DONE to quit):");
     
      //reads in user info
      sentence = readIn.readLine();
     
      //keeps receiving info till user types DONE
      while (!sentence.equals("DONE"))
      {
         tokenizer = new StringTokenizer (sentence);
         
         //keeps performing operations if the tokenizer has more words in it
         while (tokenizer.hasMoreTokens())
         {
            characterCount += word.length();
            word = tokenizer.nextToken();
                  for(int i = 0;i < word.length();i++)
                  {
                       wordStack.push(new Character(word.charAt(i)));  
                  }
         }
         sentence = readIn.readLine();
      }

      //System.out.println ("Number of words: " + wordCount);
      System.out.println ("Number of characters: " + characterCount);
     
      //Outputs contents of the stack backwards
      System.out.println ("Stack contents backwards: "+ wordStack.toString());
   
               StringBuffer sb = new StringBuffer(wordStack.size());
            while (wordStack.isEmpty() == false)
            {
                 Character c = (Character)wordStack.pop();
                 sb.append(c.charValue());
            }
   
   
   }
0
CEHJCommented:
You've got these lines

>>
            characterCount += word.length();
            word = tokenizer.nextToken();
>>

the wrong way around. What do you mean by 'not working'?
0
pete420Author Commented:
i meant it wasnt doing what i intended. it is working now when i switched those lines about. thanks

any pointers on how i could get it to work with a stack throughout? i know u cant give me the answer because its uni work but a hint would be much appreciated.
ta
pete
0
pete420Author Commented:
also it returns each character witha  space between them. ie test backwards is t s e t instad of  tset

thanks


pete
0
CEHJCommented:
>>any pointers on how i could get it to work with a stack throughout?

You just have ;-)

>>also it returns each character witha  space between them.

At what point?
0
pete420Author Commented:
lol, thanks  :-D

as for the 2nd point:
i presume its the line
                 Character c = (Character)wordStack.pop();
that prints out the characters onto the screen. it leaves a space between the characters of a word so each character has one space between it and the next.

also how does this output anything when there is no print statement?

pete
0
CEHJCommented:
>>it leaves a space between the characters of a word so each character has one space between it and the next.

Shouldn't do.

>>
             StringBuffer sb = new StringBuffer(wordStack.size());
          while (wordStack.isEmpty() == false)
          {
               Character c = (Character)wordStack.pop();
               sb.append(c.charValue());
          }
>>

Tell me what

System.out.println(sb);

prints after that
0
CEHJCommented:
If you mean there are spaces *before* reversal, on inspecting the stack, then there's something wrong with the stack class probably
0
pete420Author Commented:
sorry it was my mistake, i was still outputting the characters off the stack by saying:

System.out.println ("Stack contents backwards: "+ wordStack.toString());

instead of saying:
System.out.println(sb);


i havent used StringBuffers before but i spose there isnt any other way to do this task without it..

points are yours. the help was much appreciated!! :-D
0
CEHJCommented:
:-)

>>
i was still outputting the characters off the stack by saying:

System.out.println ("Stack contents backwards: "+ wordStack.toString());
>>

Even so, there shouldn't really have been any spaces ;-)
0
pete420Author Commented:
sorry to bother again but im still havin a small problem with this and was wondering would u help?

my code is:
import java.io.*;
import java.util.StringTokenizer;

public class wordReverse
{

   public static void main (String[] args) throws IOException
   {
      int wordCount = 0, characterCount = 0;
      String sentence, word;
      StringTokenizer tokenizer;
       
      BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in));
     
      //creates a new stack
      LinkedStack wordStack = new LinkedStack();
     
      //requests info from user
      System.out.println ("Please enter a sentence (type DONE to quit):");
     
      //reads in user info
      sentence = readIn.readLine();
     
      //keeps receiving info till user types DONE
      while (!sentence.equals("DONE"))
      {
         tokenizer = new StringTokenizer (sentence);
         
         //keeps performing operations if the tokenizer has more words in it
         while (tokenizer.hasMoreTokens())
         {
               word = tokenizer.nextToken();
               characterCount += word.length();

               for(int i = 0;i < word.length();i++)
               {
                    wordStack.push(new Character(word.charAt(i)));  
               }
         }
         sentence = readIn.readLine();
      }

      //System.out.println ("Number of words: " + wordCount);
      System.out.println ("Number of characters: " + characterCount);
     
      //Outputs contents of the stack backwards
      System.out.println ("Stack contents backwards: "+ wordStack.toString());
   
             StringBuffer sb = new StringBuffer(wordStack.size());
          while (wordStack.isEmpty() == false)
          {
               Character c = (Character)wordStack.pop();
               sb.append(c.charValue());


          }
                  System.out.println(sb);
   }
   }


the output for this can be viewed at: http://www.newwavesound.co.uk/java/output.bmp

im just puzzled as u sed it outputs properly for you by using the toString method.

if i entered 'test this' i would expect 'siht tset' to be outputted exactly like this....

thanks again

pete
0
pete420Author Commented:
ignore the above. there was a space being put in my LinkedStack..


only one problem remeains how do i get a space between the words. ie. my output now reads 'sihttset' instead of 'siht tset'

pete
0
CEHJCommented:
Well you need to retain the space delimiter as well. Use the following ctor:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html#StringTokenizer(java.lang.String, java.lang.String, boolean)


0
pete420Author Commented:
thank you,
its working perfect now

:-D
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.