- public class IfNum {
- public static void main(String[] args) {
- String str1 = "123";
- String str2 = "1a3";
- System.out.println("123是數字嗎?:"+isInteger(str1));
- System.out.println("1a3是數字嗎?:"+isInteger(str2));
- }
- //經過捕獲異常來判斷字符串是否爲數字格式
- public static boolean isInteger(String value) {
- try {
- Integer.parseInt(value);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }