阅读量:0
- 验证用户输入的字符串是否为数字:
public boolean isNumeric(String str) { if (str == null) { return false; } try { Double.parseDouble(str); return true; } catch (NumberFormatException e) { return false; } }
- 遍历字符串数组,找出所有为数字的元素:
public List<String> findNumericElements(String[] array) { List<String> numericElements = new ArrayList<>(); for (String str : array) { if (isNumeric(str)) { numericElements.add(str); } } return numericElements; }
- 检查文件中每一行是否为数字:
public List<String> findNumericLines(File file) { List<String> numericLines = new ArrayList<>(); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (isNumeric(line)) { numericLines.add(line); } } } catch (FileNotFoundException e) { e.printStackTrace(); } return numericLines; }
- 验证用户输入的手机号码是否为数字格式:
public boolean isValidPhoneNumber(String phoneNumber) { // Remove all non-numeric characters String numericPhoneNumber = phoneNumber.replaceAll("[^0-9]", ""); return isNumeric(numericPhoneNumber); }