How to Convert String into int In Java

Convert string into int or respectively wrapper class Integer is very simple and common operation . During of development in our application we have to parse string to int . There are multiple methods to convert string to number.

1. Convert String to int using Integer.parseInt(String) method
2. Convert String to int using Integer.valueOf(String) method                                                        3. Convert a String to int with leading zeroes

1. Convert String to int using Integer.parseInt(String) method

Integer.parseInt(String s) method Parses the given string argument as a signed decimal integer. The characters in the given string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ (‘\u005Cu002D’) to indicate a negative value in given string  or an ASCII plus sign ‘+’ (‘\u005Cu002B’) to indicate a positive value in given string . The resulting integer value is returned .

Syntax :

public static int parseInt(String s){

      }

Parameters: It takes parameter String s  containing the int representation to be parsed
Returns Value : This is return integer value represented by the argument in decimal.
Exception : Its Throws NumberFormatException if the string does not contain a parsable integer.

Example 1: If we use String “125” , then we will get int value 123 .

public class StringToIntByParseInt {
	
	public static void main(String[] args) {
		String str ="125";
		int num = Integer.parseInt(str); 
		System.out.println("num is  ="+ num);
	}
}

Output :

num is  =125

Example 2 : If we will take String “-123” , then it will convert string into negative number -123 .

public class StringToIntByParseInt {
	
	public static void main(String[] args) {
		String str ="-125";
		int num = Integer.parseInt(str); 
		System.out.println("num is  ="+ num);
	}
}

Output :

num is  =-125

Example 3 : If we use any string with no-digit value , then we get NumberFormatException

public class StringToIntByParseInt {
	
	public static void main(String[] args) {
		String str ="125a";
		int num = Integer.parseInt(str); 
		System.out.println("num is  ="+ num);
	}
}

Output :

Exception in thread "main" java.lang.NumberFormatException: For input string: "125a"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at com.jv.stringprogram.StringToIntByParseInt.main(StringToIntByParseInt.java:7)

We have discussed how to parse string to number in java using Integer.parse(String s) method .

2. Convert String to int using Integer.valueOf(String) method

Integer.valueOf(String s) method returns an Integer object holding the value of the specified String. valueOf() method convert string into integer in java . Integer is wrapper class of int primitive type.

Syntax :

public static Integer valueOf(String s)  {
    
   }

Parameters : This takes s the string to be parsed.
Returns value : Its return an Integer object holding the value represented by the string argument.
Exception : Its throws NumberFormatException if the string cannot be parsed as an integer.

Example 1: Now we will convert string into int in java using Integer.valueOf(String s) method

public class StringToIntByValueOf {
	public static void main(String[] args) {
		String str = "125";
		int num = Integer.valueOf(str);
		System.out.println("num =" + num);
	}
}

Output :

num =125

Example 2: In this example we see about negative integer . If we take String “-123”  , then we get int -123 .

public class StringToIntByValueOf {
	public static void main(String[] args) {
		String str = "-125";
		int num = Integer.valueOf(str);
		System.out.println("num =" + num);
	}
}

Output :

num =-125

Example 3: If String have any non-digit character , then we get NumberFormatException .

public class StringToIntByValueOf {
	public static void main(String[] args) {
		String str = "125a";
		int num = Integer.valueOf(str);
		System.out.println("num =" + num);
	}
}

Output :

Exception in thread "main" java.lang.NumberFormatException: For input string: "125a"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.valueOf(Unknown Source)
	at com.jv.stringprogram.StringToIntByValueOf.main(StringToIntByValueOf.java:6)

3. Convert a String to int with leading zeroes

The format() method of the String class in java is used to convert string to integer with leading zeros.In this example String.format() method converting string to integer without losing leading zeros .

public class StringToInt{
   public static void main(String args[]){
       String str="000123";
       /* String to int conversion with leading zeroes
        * the %06 format specifier is used to have 6 digits in
        * the number, this ensures the leading zeroes
        */
       str = String.format("%06d", Integer.parseInt(str)+200);
       System.out.println("Output String: "+str);
   }
}

Output :

Output String: 000323

If you are losing leading 0” zeros when converting to int from string , then use String.format() method.

In this tutorial we have discussed multiple ways for parse string to number java . If you are looking more string program then see String interview program for developer.

Leave a Reply

Your email address will not be published. Required fields are marked *

89 + = 91