简体   繁体   中英

how to check user input a string or number using switch statement in java?

I'm working on scanner code. How can I check the file contains number or strings by using switch statement? Here is my switch statement part code

while(st.hasMoreTokens()){
    int caret=jTextArea1.getCaretPosition();
    switch(st.nextToken()){  
    case "++": jTextArea1.insert("++"+" "+":Unary Operator "+"\n",caret);
               break;
    case "--": jTextArea1.insert("--"+" "+":Unary Operator "+"\n",caret);
               break;
    }
}

Write a method which will return true if the input param is a number other wise it will throw NFE and as a result it will return false :

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    } 
    return true;
}

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