1

I'd like to point out that I'm very new to Java, which is why I may be making stupid mistakes.

I have a class called "Characters", which consists of 4 variables and multiple methods. All variables are private, so from what I've read, I need to use methods to do anything to them.

One of the methods is supposed to return one of the variables in string form, however I keep getting an error from both eclipse and when I run it. "This method must return a result of type "String". The error occurs on the first line of the method:

public String displayStats(String option) {
    switch (option) {
    case "charName":
        System.out.println(charName);
        return charName;
    case "charHealth":
        System.out.println(charHealth);
        String charHealth2 = Integer.toString(charHealth);
        return charHealth2;
    case "charMana":
        System.out.println(charMana);
        String charMana2 = Integer.toString(charMana);
        return charMana2;
    case "charStamina":
        System.out.println(charStamina);
        String charStamina2 = Integer.toString(charStamina);
        return charStamina2;
    default:
        System.out.println("Error on default");}
}

}

The full class:

package basics;

public class Characters {

    private String charName = "";
    private int charHealth = 0;
    private int charMana = 0;
    private int charStamina = 0;

    public void summoner(Characters player) {
        player.charName = "Summoner";
        player.charHealth = 80;
        player.charMana = 150;
        player.charStamina = 50;}

    public void sentinel(Characters player) {   
        player.charName = "Sentinel";
        player.charHealth = 200;
        player.charMana = 50;
        player.charStamina = 100;}

    public void beserker(Characters player) {
        player.charName = "Beserker";
        player.charHealth = 100;
        player.charMana = 0;
        player.charStamina = 200;}

    public void mage(Characters player) {
        player.charName = "Mage";
        player.charHealth = 80;
        player.charMana = 200;
        player.charStamina = 20;}   

    public String displayStats(String option) {
        switch (option) {
        case "charName":
            System.out.println(charName);
            return charName;
        case "charHealth":
            System.out.println(charHealth);
            String charHealth2 = Integer.toString(charHealth);
            return charHealth2;
        case "charMana":
            System.out.println(charMana);
            String charMana2 = Integer.toString(charMana);
            return charMana2;
        case "charStamina":
            System.out.println(charStamina);
            String charStamina2 = Integer.toString(charStamina);
            return charStamina2;
        default:
            System.out.println("Error on default");}
    }

}
4
  • 4
    What do you think will be returned to the caller if it hits that default case? Commented Feb 9, 2016 at 18:09
  • 2
    Normally you would break from each case statement, and have a single return from your method at the end. Multiple returns from a single method is considered bad practice, as it makes the code harder to follow Commented Feb 9, 2016 at 18:11
  • I think I may have focused too much on the declaration of the method. Thank you. Commented Feb 9, 2016 at 18:12
  • How does this even compile when all paths don't return a value in the method? Maybe you should check you compiler error/warning settings. Commented Feb 9, 2016 at 18:22

4 Answers 4

1

You aren't returning anything in the default case of your switch statement, which means that there is a possibility (however small) that the method won't know what to return.

Sign up to request clarification or add additional context in comments.

Comments

0

In the displayStats function you don't return a String in all paths of your code.
This is because the default doesn't return at all.

Maybe you wanted to write:

default:
    return "Error on default";

Comments

0

Two problems: charName is a string, but charHealth, charMana, and charStamina are ints. Thus, your displayStats function isn't always returning a string.

Also, your default option in your switch statement should return a string as well.

It would be better to create an accessor function for each variable:

public String getCharName() {
    return charName;
}

public int getCharHealth() {
    return charHealth;
}

etc.

Comments

0

The method displayStats must always return a String or throw an exception. Since the code implies that the default case is an error, then throw an exception. At this point rather than create a new class of exception, just throw an IllegalArgumentException -- new IllegalArgumentException(option). When printed out it will state the type of exception and the value of the invalid option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.