Java Program to Convert String to Int

Write a Java program to convert string to int or integer. In Java, we can use Integer.parseInt and Integer.valueOf methods to convert string to an integer. In this example, we use the Integer parseInt method.

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 integer in Java.

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

This Java example uses the Integer valueOf method to convert the string to an integer.

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 string text to an integer using Java parseInt. It will throw the exception.

package NumPrograms;

public class StringToInt4 {
	
	public static void main(String[] args) {
		
		String s1 = "tutorialgateway";
		
		int x = Integer.parseInt(s1);
		System.out.println(x);
	}
}
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)

Trying to convert string text to int using the Java valueOf, and the program throws the error.

package NumPrograms;

public class StringToInt5 {
	
	public static void main(String[] args) {
		
		String s1 = "Java Programs";
		
		int x = Integer.valueOf(s1);
		System.out.println(x);
		
	}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "Java Programs"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:766)
	at NumPrograms.StringToInt5.main(StringToInt5.java:9)

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 String to Convert =  ");
		s1 = sc.nextLine();
	
		int x = Integer.valueOf(s1);
		System.out.println("Integer.valueOf(s1) result  = " + x);
		
		int y = Integer.parseInt(s1);
		System.out.println("Integer.parseInt(s1) result = " + y);
	}
}
Please Enter any String to Convert =  13476
Integer.valueOf(s1) result  = 13476
Integer.parseInt(s1) result = 13476