eniniz eniniz - 1 year ago 54
Java Question

how to convert String into Integer in java

I want to convert


If the String contain character it will return 0
Else it will return the number
For example the conversion of (ad12) is 0
But (12) Is 12

Answer Source

Try following:

public int getInt(String test){
            return Integer.parseInt(test.trim());
        }catch(Exception e){
            return 0;

All you need to do is use Integer class to get int value of valid integer containing string.