user949902
user949902

Reputation: 2711

Java int return string

I have an int variable and I want to create a String method to return that int variable, how do I go about it? Example below... and set the getAge() method to return "young" when age is 18, "old" when age is 30.

private int age;

public String getAge() {

}

Upvotes: 2

Views: 7797

Answers (4)

hellojava
hellojava

Reputation: 5064

Better use a switch statement since the conditonal expression is not changing. So do it like:

switch (age) {
    case 18:
        return "young";
    case 30:
        return "old";
    default:
        return "??";
    }

Upvotes: -1

cwallenpoole
cwallenpoole

Reputation: 82058

Taken literally:

public String getAge() {
    return (30 == age)? "old":
           (18 == age)? "young":
                // because you said 18 is young, 30 is old, but didn't say
                // anything about all of the other ages!
                "I don't understand!";
}

You can do this a few ways. Ternary structure and "if" statement are generally the best.

// this if/else reads "(if age >= 30 then return old) else return young"
public String getAge() {
    if (30 <= age)
       return "old";
    else
       return "young";
}

// this ternary statement reads "return (if age >= 30 then old) else young"
public String getAge() {
    return (30 <= age)? "old":"young";
}

// This would be my preference
public String getAge() {
    // add bounds checking!
    if (125 <= age)
       return "You are probably dead";
    else if (0 > age)
       return "Hi doc brown! What's it like to travel through time?";
    else if (30 <= age)
       return "old";
    return "young";
}

Upvotes: 5

Robin
Robin

Reputation: 24272

I would not recommend this for the trivial case of just 2 ages, but if you want to expand...

You can of course also add display strings as well.

public enum AgeMonikers
{
    AweCute(2),
    DontTouchThat(4),
    Child(10),
    Preteen(13),
    Trouble(20),
    MoveOut(24),
    ThinkYouKnowEverythingDev(25),
    ActuallyKnowSomeDev(30),
    OldFart(100),
    WishIWasDead(Integer.MAX_VALUE);

    private int maxAge;

    private AgeMonikers(int ageLimit)
    {
        maxAge = ageLimit;
    }

    static public AgeMonikers getMoniker(int age)
    {
        if (age < 0) 
            return null;

        for(int i=values().length-1; i>0; i--)
        {
            AgeMonikers val = values()[i];

            if (age >= val.maxAge)
                return values()[i+1];
        }
        return AweCute;  // age < 2 - I know it will include negatives.
    }
}


public String getAge() 
{
    return AgeMoniker.getMoniker(age).toString();
}

Upvotes: 0

Kit Ho
Kit Ho

Reputation: 26998

getAge() is not a good naming method.

It confused other developers/users that getAge() will return an int number.

I think you should name your method like getAgeClass().

Notice that a public method will be exposed to other classes, it is very important your naming of public method should be meaningful, not confusing. This is a good practice when you code OO

Upvotes: 0

Related Questions