Results 1 to 7 of 7
  1. #1
    avirunes is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Missing Return statement =[

    hey i get the following compile error, and im not sure what to do to fix it.

    src\server\models\players\Playerassistant.java:167 2: missing return statement
    }
    ^

    this is the code.

    public String getSkillName(int skillId) {
    switch (skillId) {
    case 0:
    return "Attack";
    case 1:
    return "Defence";
    case 2:
    return "Strength";
    case 3:
    return "Hitpoints";
    case 4:
    return "Ranged";
    case 5:
    return "Prayer";
    case 6:
    return "Magic";
    case 7:
    return "Cooking";
    case 8:
    return "Woodcutting";
    case 9:
    return "Fletching";
    case 10:
    return "Fishing";
    case 11:
    return "Firemaking";
    case 12:
    return "Crafting";
    case 13:
    return "Smithing";
    case 14:
    return "Mining";
    case 15:
    return "Herblore";
    case 16:
    return "Agility";
    case 17:
    return "Theiving";
    case 18:
    return "Slayer";
    case 19:
    return "Runecrafting";

    }
    }


    line 1672 is the first bracket.

    Thanks, any help appreciated.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The compiler demands that the method returns a String for any value that skillId might have.

    If it is your intent that skillId only have values 0-19 then you could return some bogus value or, better, throw an exception. The best thing would be to use something other than int as the type of skillId: this is what enums are for.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Try to use break; in your code

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    And finaly

  5. #5
    avirunes is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    if i put a number, i get imcompatible types, found: int, if i put a name i get cannot find symbol xxxx variable :S

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by milovan View Post
    And finaly
    That would be a very silly solution; the OPs example is equivalent to this code:

    Java Code:
    String foo(int i) {
       switch (i) {
          case 1: return "one";
          case 2: return "two";
          case 3: return "three";
       }
    }
    Even if everybody swears that method foo only will be called with a parameter 1, 2 or 3 the compiler won't believe them because (obviously) method foo doesn't return a String value for any parameter value not in the range 1, 2 or 3. Even more, this code:

    Java Code:
    String foo(int i) {
       if (i < 1 || i > 3) return "huh?";
       switch (i) {
          case 1: return "one";
          case 2: return "two";
          case 3: return "three";
       }
    }
    ... doesn't make the compiler shut its mouth, i.e. it still thinks that at the end of the method no String value is returned. The compiler doesn't interpret the code, nor can't it understand the semantics, the intention, of the code. Basically, when the compiler whines about "no return value" it is right and rearrange your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Basic usage:
    Java Code:
    public String getSkillName(int skillId) {
    
        String s = ""; // or some default value
    
        switch (skillId) {
    
            case 0:
                s = "Attack";
                break;
            case 1:
                s = "Defence";
                break;
            ...
    
            case 19:
                s = "Runecrafting";
                break;
        }
        return s;
    }

Similar Threads

  1. Missing return statement
    By gkoef in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 02:52 AM
  2. Missing return statement error.
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 09:15 PM
  3. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  4. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •