mardi 26 janvier 2021

NumberFormatException in Integer.Valueof(string) / String to random Int to String

so I have problém generating random number from a string. I have this code below:

    public static void main(String[] args) {
    String limit = "Maximální limit této karty je 150 000 CZK. Limit můžete stále navýšit o 100 000 CZK.";
    String str = limit.replaceAll("\\u00A0", "");
    String pattern = "\\D*(\\d*\\s\\d*)\\D*(\\d*\\s\\d*)\\D*";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(str);
    if (m.find()){
        String str1 = m.group(1);
        int i = Integer.valueOf(str1);
        int a = (int)(Math.random()*i);
        String str2 = a + "";
        System.out.println(str2);
    }
    else {
        System.out.println("Nothing found");
    }
}

The error says:

Exception in thread "main" java.lang.NumberFormatException: For input string: "150 000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at com.company.Main.main(Main.java:16)

Can someone help me out pls :( I am trying to generate a random number from first string numberformat found which is 150 000.




Aucun commentaire:

Enregistrer un commentaire