my main concern with this code as of right now is a missing return statement.
public class stringstuff{
//using charAt
public static String ReverseF(String n){
String finalstring = "";
int len = n.length();
for (int i = 0; (i < n.length()); i++){
finalstring += (n.charAt(len - i - 1));
}
System.out.println(finalstring);
}
public static void main(String[]args){
ReverseF("Hello");
}
}
Using this code I only get the error:
stringstuff.java:11: missing return statement
}
^
1 error
If I switch the System.out.println to return, I don't get any errors but I also don't get an answer for ReverseF("Hello");
Let's break down the keywords in your function header:
Now note that Java thinks this function will return a string. The compiler checks to see if there is a return statement of the proper type.
Either use a void as the functio type and print the string inside of the function (as you' re doing now), or add the return statement and move the print to the place where the function is called from (as already suggested).
HTH
You have two options: either write
or write
return finalString
inReverseF
and useYou get an compilation error "missing return statement" because your return statement is missing...
Is it necessary that your method is returning a value? If not, just change it from String to void, do the printing in your method and rename it to something like "printReverseF", so that your method name indicate what it's doing!
Other feedback: