[英]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? 如何使用switch语句检查文件中包含数字或字符串? Here is my switch statement part code 这是我的switch语句的部分代码
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 : 编写一个方法,如果输入参数是一个数字,则返回true,否则它将抛出NFE,结果将返回false:
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.