Saturday 19 July 2014

convert string to int in Java

There are two basic ways to convert String to integer in Java.

1. Using Integer.parseInt method.
public class IntToString {
    public static void main(String args[]){
        String str = "123";
        int i = Integer.parseInt(str);
        System.out.println(i);
    }
}

2. Using Integer.valueOf() method
public class IntToString {
    public static void main(String args[]){
        String str = "123";
        int i = Integer.valueOf(str);
        System.out.println(i);
    }
}

                                                             Home

No comments:

Post a Comment