简体   繁体   中英

How would I make an 'if' function for a String whether to run a specific function if the string is a value or a word?

String texts;
texts = textFieldb.getText();
String textd;
textd = textFieldc.getText();

How would I get it to run a specific function using " if ", example : If 'texts' is a number then it'll run a specific function, and if it wasn't a number, it would run another. Also, how would I do that if it's a word, rather then a number? Thanks. :)

I think it's really simple. You just need to parse your String and catch the exception. If it's in the exception, that's not a number, then you can do any business you want. For example:

    String texts = "test";
    try {
        int intValue = Integer.valueOf(texts);
        System.out.println("is number");
    } catch(NumberFormatException ex) {
        System.out.println("is not a number");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM