I wrote a recursion code, but I don't know why it won't work.(I changed some of the previous mistakes, but it still won't work :( )
The problem is:
Write a recursive method which
a. prints each character of the string reversed twice b. doubleReverse("hello") prints oolllleehh
The code I have so far is as below:
public class Recursion{
public static void main(String[] args) {
String s = "hello";
doubleReverse(s);
}
public static void doubleReverse(String s) {
if (s == null || s.equals("")){
return;
}
System.out.print(s.charAt(s.length()-1) + s.charAt(s.length()-1) + doubleReverse(s.substring(1)));
}
}
The expected output doubleReverse("hello")
prints oolllleehh
The output I'm getting is: won't compile
Error message:
2 errors found:
File: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java [line: 12]
Error: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java:12: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match
File: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java [line: 12]
Error: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java:12: 'void' type not allowed here