JAVA: missing return statement

前端 未结 3 1998
旧时难觅i
旧时难觅i 2020-12-22 09:17

my main concern with this code as of right now is a missing return statement.

public class stringstuff{

    //using charAt    
    public static String Rev         


        
3条回答
  •  生来不讨喜
    2020-12-22 09:44

    You get an compilation error "missing return statement" because your return statement is missing...

    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);
            }
    
            return finalstring;
        }
    
        public static void main(String[] args) {
            System.out.println(reverseF("Hello"));
        }
    }
    

    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:

    • start method names with lowercase, so you can distinct them from Classes
    • for concatenation of Strings in loops, you should use a StringBuilder/StringBuffer. Then Java doesn't have to create new String-objects per iteration of your loop - this could be a huge performance problem!

提交回复
热议问题