0

I'm trying to make a method that takes input of any data type from user and then cast that input into a variable type. But my code always returns string type. any fix required?

Scanner x = new Scanner(System.in);
Object a = x.next();

here's my method for detecting the object type

public static String inputValidator(Object input)
{
    if(input.getClass().getName().equals("java.lang.String"))
    {
        return "string";
    }
    else if(input.getClass().getName().equals("java.lang.Integer"))
    {
        return "integer";
    }
    return "nor";


}
Munna
  • 109
  • 2
  • 12
  • Bytes can be interpreted as anything. – Sotirios Delimanolis Apr 03 '15 at 17:51
  • 5
    [`next`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()) returns a `String`. Java is strongly typed, not duck typed. This does not work. – Boris the Spider Apr 03 '15 at 17:54
  • okay so what part of my code need to be changed? I guess taking the input. what should I write instead? – Munna Apr 03 '15 at 17:55
  • 2
    Can you provide example(s) of how you would like this to work? What output you want for what input? – Scott Hunter Apr 03 '15 at 17:55
  • I'm trying to take input 'anything' from the user which will be assigned as Object and will detect the classType. – Munna Apr 03 '15 at 17:58
  • possible duplicate of [How to check if a string is numeric?](http://stackoverflow.com/questions/14206768/how-to-check-if-a-string-is-numeric) – Barett Apr 03 '15 at 18:01
  • If this is a learning exercise, then great, carry on. If your trying to do something productive use Commons CLI https://commons.apache.org/proper/commons-cli/ – MarkOfHall Apr 03 '15 at 18:09

1 Answers1

3

That's not a correct way since Scanner.next() returns a String. If you are looking for primitives or some 'basic' types then you could use an appropriate method like nextByte() or nextBigDecimal().

I.e. with your current implementation you will not ever get to the else if block which checks for Integer.

If you are mostly interested in numbers please check this: How to check if a String is numeric in Java

lets say String ,int and double only

If performance is not that much of an issue and you are looking for a simplistic solution then I would suggest to go with:

if (isInteger(string)) {
    // is an integer
} else if (isDouble(string)) {
    // is a double 
} else {
    // is a string
}

public static boolean isDouble(String string) {
    try {
        double d = Double.parseDouble(string);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

public static boolean isInteger(String string) {
    try {
        double d = Integer.parseInt(string);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

NOTE: call order matters here, you have to call isInteger() before isDouble() cause integer string is also a double string.

Community
  • 1
  • 1
S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • what are you trying to achieve? probably another question mentioned by @Barett could help? – S. Pauk Apr 03 '15 at 18:01
  • please have a look at my updated post, "I'm trying to take input 'anything' from the user which will be assigned as Object and will detect the classType" – Munna Apr 03 '15 at 18:02
  • @Munna as I have mentioned before all you get is a `String`. Do you intend to be able to recognize integers, doubles, booleans, dates etc..? I would say that you have to define a list of types you want to check for. – S. Pauk Apr 03 '15 at 18:09
  • exactly I want to be able to recognize. how can i achieve so? – Munna Apr 03 '15 at 18:13
  • @Munna could you provide a complete list of types you want to be able to parse? – S. Pauk Apr 03 '15 at 18:15
  • lets say String ,int and double only – Munna Apr 03 '15 at 18:25