Search code examples
javastringbooleansystemsymbols

Accepting foreign letters in Java?


I made a method that can detect numbers in a string. However, It does not accept strings with ÆØÅ letters. Any advice to make it accept it and easily integrated into my method?

public static boolean checkNumbers(String testString) {

        String numOnly = testString.replaceAll("\\p{Alpha}", "");
        try {
            double numVal = Double.valueOf(numOnly);
            System.out.println(testString + " contains numbers");
            return true;
        } catch (NumberFormatException e) {
            System.out.println(testString + " Contains no numbers");
            return false;
        }
    }

Solution

  • As posted in the comments:

    you will need to change your .replaceAll("\\p{Alpha}", "");

    to .replaceAll("[^\\d.]", "");