eniniz eniniz - 6 months ago 12
Java Question

how to convert String into Integer in java

I want to convert

String
to
int

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

Try following:

public int getInt(String test){
        try{
            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.

Comments