-4

Hi I'm sort of new to java so this may be an easy solution but I need to be able to tell if user input is a String or some other object and this does not seem to work:

public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     Object name;

     //have user enter their name 
     do{
        System.out.println("Please enter your name: ");
        name = sc.nextLine();

        if(name instanceof String){
            System.out.println(name);
            break;
        }else{
            System.out.println("Enter a String!");
            sc.next();
            }
    } while(true);

}

C.Herring
  • 11
  • 2

2 Answers2

0

Scanner#nextLine() always returns a String. However, if you want to check the type of an object, you can use instanceof operator or getClass() method to determine the exact class.

For example, someObject instanceof String will return true only if it isn't null and its type is String.

If you want to convert the String into another type of object (for example - a number), you should read about type conversions. For example, check this.

Community
  • 1
  • 1
Czyzby
  • 2,999
  • 1
  • 22
  • 40
0

giving credit to AlexR (here: Parse String into Number), you could always try to use a series of if/then's to see if the String value CAN be translated to a number style (int, long, etc...)

private static Number parse(String str) {
    Number number = null;
    try {
        number = Double.parseDouble(str);
    } catch(NumberFormatException e) {
        try {
            number = Float.parseFloat(str);
        } catch(NumberFormatException e1) {
            try {
                number = Long.parseLong(str);
            } catch(NumberFormatException e2) {
                try {
                    number = Integer.parseInt(str);
                } catch(NumberFormatException e3) {
                    throw e3;
                }       
            }       
        }       
    }
    return number;
}
Community
  • 1
  • 1
arcee123
  • 101
  • 9
  • 41
  • 118
  • I guess I'm not a Java expert, but is there really no better way to do that? Because that is *awful*. – David Feb 02 '16 at 17:01
  • 2
    if Double.parseDouble throws a NumberFormatException under what circumstances would Float or Integer not through a NumberFormatException? – Keefe Roedersheimer Feb 02 '16 at 17:01
  • Sorry, re-read your question. I am giving credit to AlexR for his solution, but your case, i'd go with INT first, then the others. – arcee123 Feb 02 '16 at 17:02
  • Every Float is a Double, nothing useful can ever come from the second try/catch. Long may be a bigger value than what fits into a Double, but that is a different question. This is not a good approach. – Keefe Roedersheimer Feb 02 '16 at 17:05
  • float is larger than double, because of the number of bits used to store the number. It can handle higher numbers. if it fails double, going for float widens the aperture. – arcee123 Feb 02 '16 at 17:07
  • please see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html you have that reversed – Keefe Roedersheimer Feb 02 '16 at 17:08
  • yep, reversed. I was reading 038 and 308 in the exponent. Same theory though. using one in the narrow aperture, then expanding the aperture by using the other assists in memory collection, since it takes less memory. However, if you WANT to specify one first, then you can in this example. – arcee123 Feb 02 '16 at 17:11
  • Instead of that it's always possible to use a neat isNumeric method from apache commons: https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isNumeric(java.lang.CharSequence) – Enigo Feb 02 '16 at 17:36