WinSide
WinSide

Reputation: 11

Return integer in function String

How return integer in function String

It's my code

public class Utils {
    public static String type(String s){
        if(UraniumProtectMain.materialname){
            return s;
        } else {
            return Integer.parseInt(s); //<--- It's
        }
    }
}

Upvotes: 1

Views: 107

Answers (3)

user3037028
user3037028

Reputation: 76

As you want to return both Integer and String, you can use return type as Object class, because Object is super class of all classes and we can hold any type of value in Object variable.

public static Object type(String s){    
    // code here
}

Now you can get the type of value return by type with the help of instanceOf method

if(Utils.type() instaceOf String)
{
    // code here
} else if(Utils.type() instanceOf Integer) {
    // code here
}

Upvotes: 1

Moosa
Moosa

Reputation: 79

As User has suggested that you can return change method return type to object. But, apart from that you can also use either of

  1. String.valueOf(number) (my preference)
  2. Integer.toString(number)

to return integer as string and perform type check at method calling point

Upvotes: 0

Payal Bansal
Payal Bansal

Reputation: 755

Either return Object or return integer as String.

Upvotes: 1

Related Questions