Posts

Showing posts from November, 2014

How to convert an String into int value

There are many ways to convert an String into Integer object in Java e.g. Integer.valueOf(), new Integer(), Integer.parseInt() or writing your own algorithm to convert string to int etc, but which one is the best tool for the job. here are different ways of converting String object into int or Integer in Java : 1) By using Intger.parseInt(String string-to-be-converted) method This is my preferred way of converting an String to int in Java, Extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:  //using Integer.parseInt  int i = Integer.parseInt("123");  System.out.println("i: " + i); Integer.parseInt() method will throw NumberFormatException if String provided is not a proper number. Same technique can be used to convert other data type like float and Double to String in Java. Java API provides static methods like Float.parseFloat() and Double.parseDouble() to perform data type conversion. ...