Q:

Java Convert String to int

belongs to collection: Java Conversion Programs

0

We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which returns instance of Integer class.

Scenario

It is generally used if we have to perform mathematical operations on the string which contains a number. Whenever we receive data from TextField or TextArea, entered data is received as a string. If entered data is in number format, we need to convert the string to an int. To do so, we use Integer.parseInt() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Signature

The parseInt() is the static method of Integer class. The signature of parseInt() method is given below:

 

public static int parseInt(String s)  

Java String to int Example: Integer.parseInt()

Let's see the simple code to convert a string to an int in java.

 

int i=Integer.parseInt("200");  

Let's see the simple example of converting String to int in Java.

//Java Program to demonstrate the conversion of String into int  
//using Integer.parseInt() method  
public class StringToIntExample1{  
public static void main(String args[]){  
//Declaring String variable  
String s="200";  
//Converting String into int using Integer.parseInt()  
int i=Integer.parseInt(s);  
//Printing value of i  
System.out.println(i);  
}}  

 

Output:

200

 

Understanding String Concatenation Operator

//Java Program to understand the working of string concatenation operator  
public class StringToIntExample{  
public static void main(String args[]){  
//Declaring String variable  
String s="200";  
//Converting String into int using Integer.parseInt()  
int i=Integer.parseInt(s);  
System.out.println(s+100);//200100, because "200"+100, here + is a string concatenation operator  
System.out.println(i+100);//300, because 200+100, here + is a binary plus operator  
}}  

 

Output:

200100
300

 

Java String to Integer Example: Integer.valueOf()

The Integer.valueOf() method converts String into Integer object. Let's see the simple code to convert String to Integer in Java.

//Java Program to demonstrate the conversion of String into Integer  
//using Integer.valueOf() method  
public class StringToIntegerExample2{  
public static void main(String args[]){  
//Declaring a string  
String s="200";  
//converting String into Integer using Integer.valueOf() method  
Integer i=Integer.valueOf(s);  
System.out.println(i);  
}}  

 

Output:

300

 

NumberFormatException Case

If you don't have numbers in string literal, calling Integer.parseInt() or Integer.valueOf() methods throw NumberFormatException.

//Java Program to demonstrate the case of NumberFormatException  
public class StringToIntegerExample3{  
public static void main(String args[]){  
String s="hello";  
int i=Integer.parseInt(s);  
System.out.println(i);  
}}  

 

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hello"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.parseInt(Integer.java:770)
	at StringToIntegerExample3.main(StringToIntegerExample3.java:4)

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert int to String... >>