Java Program to Convert String to Int

Write a Java program to convert string to int or integer using valueOf() and parseInt() methods with an example. In this programming language, we can use parseInt or valueOf methods to convert a string to an integer or int.

Java Program to Convert String to Int or integer

In this example, we declared two strings, s1 and s2, with numeric values. Next, we used the Integer parseInt() method to convert them.

package NumPrograms;

public class StringToInt1 {
	
	public static void main(String[] args) {
		
		String s1 = "2020";
		String s2 = "10";
		
		int x = Integer.parseInt(s1);
		int y = Integer.parseInt(s2);

		System.out.println(x);
		System.out.println(y);
	}
}
Java Program to Convert String to Int

Let me perform the addition before and after converting the string to an integer using the built-in parseInt() function.

package NumPrograms;

public class StringToInt2 {
	
	public static void main(String[] args) {
		
		String s1 = "1040";
		
		int x = Integer.parseInt(s1);

		System.out.println(s1 + 50);
		System.out.println(x + 50);
	}
}
104050
1090

Convert String to Int using valueOf() function

This example uses the Integer valueOf() method to convert the string to an integer. As you can see from the below code, we performed addition after converting the value.

package NumPrograms;

public class StringToInt3 {
	
	public static void main(String[] args) {
		
		String s1 = "2000";
		
		int x = Integer.valueOf(s1);
		System.out.println(x);
		
		System.out.println(s1 + 150);
		System.out.println(x + 150);
	}
}
2000
2000150
2150

Let us try to convert the pure text characters to an integer using the parseInt and valueOf functions. Both these functions will throw the exception. So, please use the try cache block to handle these exceptions.

public class Example {

	public static void main(String[] args) {

		String s1 = "tutorialgateway";

		int x = Integer.parseInt(s1);
		System.out.println(x);

		int y = Integer.valueOf(s1);
		System.out.println(y);
	}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "tutorialgateway"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)
	at NumPrograms.StringToInt4.main(StringToInt4.java:9)

Using parseInt() and valueOf() functions

This Java example accepts the user input string value and converts it to an integer using valueOf and parseInt functions.

package NumPrograms;

import java.util.Scanner;

public class StringToInt6 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		String s1;
		sc= new Scanner(System.in);

		System.out.print("Please Enter any Text =  ");
		s1 = sc.nextLine();
	
		int x = Integer.valueOf(s1);
		System.out.println("valueOf(s1) result  = " + x);
		
		int y = Integer.parseInt(s1);
		System.out.println("parseInt(s1) result = " + y);
	}
}
Java Program to Convert String to Int using parseInt and valueof