0

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

  • 2
    Where is that code from? Is that from your task description or not? There are *several* things wrong (apart from the missing parts). 1. you *define* a `String` but *assign it* a `Sentence` object. 2. you seem to try to call a static method `String.reverse` when you *probably* wanted to do something like `greeting.reverse()`. – Joachim Sauer Feb 20 '13 at 16:29
  • The code i think was a barebones for what is supposed to be done. Basically I am just supposed to take a string(Hello!), reverse it, then print it out.. It should print out: (!olleH) – user2092082 Feb 20 '13 at 16:32
  • 1
    possible duplicate of [Whats the best way to recursively reverse a string in Java?](http://stackoverflow.com/questions/859562/whats-the-best-way-to-recursively-reverse-a-string-in-java) – Adam Arold Feb 20 '13 at 16:40
  • @user2092082: that code, as you posted it, can't work at all, no matter what other code you wrote. If that's in your problem description then it is broken (either intentionally or not). If it's *your interpretation* of the problem description, then that's a different issue. – Joachim Sauer Feb 20 '13 at 16:43

1 Answers1

1

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.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • That worked thankyou so much.. Is there any way you could intruct me on something such as: implementing a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and then combining the two? – user2092082 Feb 20 '13 at 16:37
  • There are several questions about this already in SO. Just look at this one for example: http://stackoverflow.com/questions/9723912/reversing-a-string-recursion-java – Cyrille Ka Feb 20 '13 at 16:39