1

The following Java doesn't work as it is missing a return statement. I can't figure out what is wrong. Any Ideas?

public String setusername(String u) {    
    if (username.length() > usernameLimit) {
        System.out.println("overlimit");
    } else {
        return this.username = u;
    }
}

Even if I take out the string u it gives the same error and if i add int usernameLimit it gives the same error.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2228777
  • 117
  • 1
  • 1
  • 7

4 Answers4

1

Your code uses branching(if loop). From the compilers perspective both branches i.e. the if and else blocks are likely to be executed. But the return is only in else block. Hence the compiler complains about missing return.

Solution1: Try to put return in If block as well (based on your requirement)

Solution2: Move your return out of the If-Else construct. You could use a variable to designate return value and populate it accordingly in both if or else.

prashant
  • 1,805
  • 12
  • 19
1

your return statement is in else block, so compiler doesn't know whether at runtime function will return something or not.

changing it to

public String setusername(String u) {
String result="overlimit";
if (username.length() <= usernameLimit) {
    this.username = u;
    result=u;
}
return result;
}

should work fine

Ashish Thukral
  • 1,445
  • 1
  • 16
  • 26
0
if (username.length() > usernameLimit) {
    System.out.println("overlimit");
    ////////////return what??? 
 }

here is the problem , the method need to return statement when username.length() > usernameLimit

Alya'a Gamal
  • 5,624
  • 19
  • 34
0

Assuming this question is related to your previous question, Java String Limit, you probably do not need to use a return statement in setusername() method.

import java.io.*;

public class UserNameTest {
    private static final int USER_NAME_LIMIT=6; // constants should be all UPPERCASE_WITH_UNDERSCORES

    private String userName; // variable names should be in camelCase

    private String getUserName() {
        // normally you write a getter method to obtain value
        return userName;
    }

    private void setUserName(String u) {
        // setter method would not require a return statement,
        //   if all you trying to do is to validate and set the new value.

        if (u.length() > USER_NAME_LIMIT) { // validate length of new value
            // good practice to be descriptive when giving error messages
            System.out.println("Given user name [" + u + "] is longer than valid limit [" + USER_NAME_LIMIT + "]");
        } else {
            this.userName = u;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String inputString = br.readLine();

        UserNameTest q1 = new UserNameTest();
        q1.setUserName(inputString); // set user name retrieved through input

        System.out.println("New user name: " + q1.getUserName()); // get user name for other operations
        // if setUserName() above rejected the input value due to length exceeding, here you will see 'null'
        //   as we have not set user name to anything beforehand
    }

}

Read and learn about coding standards as well http://geosoft.no/development/javastyle.html

Community
  • 1
  • 1
Sithsu
  • 2,209
  • 2
  • 21
  • 28