简体   繁体   中英

Recursion String in Java and removing characters

Here is what I have so far, but DrJava doesnt seem to compile it. It is supposed to print out "!olleH".. and I keep getting these errors: cannot find symbol : class Sentence, cannot find symbol : method reverse(), cannot find symbol : method getText().

public class SentenceTester
{
   public static void main(String[] args)
   {
      String greeting = new Sentence("Hello!");
      String.reverse();
      System.out.println(greeting.getText());
   }
}

I am also supposed to(once this is running) implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two.

I am not asking for my homework to be done but better yet a barebones code because I really dont know where to start.

Thanks

There is no reverse() method in String class. You can use a StringBuilder instead:

String greeting = "Hello!"
StringBuilder sb = new StringBuilder(greeting);
greeting = sb.reverse().toString();

See StringBuilder.reverse() .

Also, I don't know what the Sentence class is, but you probably can't initialize a String with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM