I am learning recursion and I am trying to create a reverseString
method without loops. When declaring a variable to be manipulated, I get these two errors:
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
2 errors
Does anyone know why? Here is my code:
public class MyClass {
int static count = 1;
public static String reverseString(String str) {
String reverse = "";
String sub = str.substring(str.length() - count, str.length() - count 1);
reverse = sub;
if (sub.length() != 1) {
return reverseString(str.substring(0, str.length() - count));
} else {
return reverse;
}
count ;
}
public static void main(String args[]) {
System.out.println(reverseString("Hello"));
}
}
CodePudding user response:
The modifiers (like static
) should come before the type:
static int count = 1;