Social Question

charpays's avatar

How can I use the reverse method of the string buffer using Java?

Asked by charpays (1points) March 3rd, 2010

How can I use the reverse method of the string buffer using Java?

how can I have the user enter a string then using the reverse method, reverse the string.
For example, the method will display

?uoy era woH

for the input

How are you?

Repeat the operation until an empty string is entered.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package stringrevchar;

import java.util.Stack;
import java.util.StringTokenizer;

/**
*
* @author n/a
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// StringRevChar.java
String sh = “FCGDAEB”;
System.out.println(sh + ” -> ” + new StringBuffer(sh).reverse( ));

String s = “How are you?”;
// Put it in the stack frontwards
Stack myStack = new Stack( );
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) myStack.push(st.nextElement( ));
// Print the stack backwards
System.out.print(’”’ + s + ’”’ + ” backwards by word is:\n\t\””);
while (!myStack.empty( )) {
System.out.print(myStack.pop( ));
System.out.print(’ ’);
}
System.out.println(’”’);
}

}

I used a stack but I need to do it using the reverse method instead , what is the correct code?

Observing members: 0 Composing members: 0

12 Answers

MrItty's avatar

Show your attempt at using reverse() yourself, and one of us might help you fix/improve it. No one is going to write your homework for you.

charpays's avatar

I already did dont you see the code…

MrItty's avatar

Did you see your own question?

“I used a stack but I need to do it using the reverse method instead”

Where is your attempt at using the reverse method? You’ve shown code that you claimed to write that uses a stack. You’re telling us you need to not use the stack, but use reverse(). So show us your attempt to do that, and I’ll be more than happy to help you correct it.

charpays's avatar

I dont know how to do it thee other way the book does not explain, that why im asking duh

charpays's avatar

reads a sentence and prints out the sentence in reverse order using the reverse method of the StringBuffer class. For example, the method will display

?uoy era woH

for the input

How are you?

charpays's avatar

System.out.print(“Reversed string : ”);
System.out.println(strbuf); //print dlroW avaJ_olleH
strbuf.reverse();
System.out.println(strbuf); //print Hello_Java World

charpays's avatar

thats all i have

timtrueman's avatar

“StringBuffer(string).reverse()” should get you on the right track.

charpays's avatar

“StringBuffer(string).reverse()” – where do i put this in my code was my other code correct?

timtrueman's avatar

It seems like you’re just looking for a copy and paste answer and we’re not going to give that to you since this seems to be a homework problem. I’m not really sure what else to tell you other than just try stuff until it works.

charpays's avatar

I already wrote out the code youre not really helping I was reffered here, I think this site sucks so far no one is helping just saying they wont help.

MrItty's avatar

This is not a “do my homework for me” site. This is a question and answer site. If you were lead to believe it is a “do my homework for me” site, you should complain to the person who referred you.

No one is saying they won’t help. On the contrary, we are saying we are very willing to help. What we are saying is that we won’t do it for you. There is a large difference.

Answer this question

Login

or

Join

to answer.
Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther