GreenTea
GreenTea

Reputation: 35

Can I convert a String into Integer or assign it with a value?

I wanna ask if I can convert a String into an int or give the String a value.

Here is my Code: In my first line of the Method I generate a customer number for example "KU605-43", "CU629-34", "YT634-45".... as a String,

In the next line I take out my number for example of the first example 60543.

I try to ParseInt ValueOf and so on but it still does not work.

public int wertigkeitUeberpruefen(){
    String str = generate();
    str = str.replaceAll("\\D+","");
}

Upvotes: 0

Views: 49

Answers (2)

Trevor Clarke
Trevor Clarke

Reputation: 1478

For me the following works, where value is the number.

public int wertigkeitUeberpruefen(){
     String str = generate();
     str = str.replaceAll("\\D","");
     int value = Integer.parseInt(str);
}

Upvotes: 0

yogur
yogur

Reputation: 820

First remove all non-numeric characters. Then parse the string to an integer and return it.

public int stringToParse(String str){
    str = str.replaceAll("[^\\d]", "");
    return Integer.parseInt(str);
}

Upvotes: 2

Related Questions