Sunday, January 26, 2014

How to convert a string to int?

Source:
public class ConvertStringToInt {
   public static void main(String[] args) {

      String str = "123456";
      int x = Integer.parseInt(str);
 
      System.out.println("String: " + str);
      System.out.println("Type: " + str.getClass().getSimpleName());

      System.out.println();

      System.out.println("int: " + x);
      if (x == (int)x ) {
         System.out.println("Type: int");
      }
   }
}

Output:
   $ java ConvertStringToInt 
   String: 123456
   Type: String

   int: 123456
   Type: int

Blog Archive