0

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;
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Asher Khan
  • 23
  • 6
  • Possible duplicate of [What's the best way to check to see if a String represents an integer in Java?](http://stackoverflow.com/questions/237159/whats-the-best-way-to-check-to-see-if-a-string-represents-an-integer-in-java) – Aaron Oct 23 '15 at 09:37

1 Answers1

0

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;
}
Rahman
  • 3,755
  • 3
  • 26
  • 43