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);
}