Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reverse String

807600Jun 25 2007 — edited Sep 25 2007
Hi
I am very new to java and i am trying to reverse a string of text, the coe below works perfectly but i need the text to be input at the command prompt window and then it shows the output fo the reversed text as opposed tothe way i have set the code up below. does that make sense??

I basically need to have a line that says please enter your text to be reversed:
and it then reverses.

Thanks

public class TestStringReverse {

public static void main(String[] args) {
String string = "Enter string to be reversed";
String reverse = "";

int len = string.length();
if(len == 1) {
reverse = string;
} else {
for(int count = string.length()-1; count >= 0; count-- ) {
reverse += string.charAt(count);
}
}

System.out.println(reverse);
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 23 2007
Added on Jun 25 2007
27 comments
1,085 views