Java Tutorials
Java Math Abs() Round() Ceil() Floor() Min() Methods with Example
Java has had several advanced usage application including working with complex calculations in...
There are two ways to convert String to Integer in Java,

String strTest = “100”;Try to perform some arithmetic operation like divide by 4 – This immediately shows you a compilation error.
class StrConvert{
public static void main(String []args){
String strTest = "100";
System.out.println("Using String:" + (strTest/4));
}
}
Output:
/StrConvert.java:4: error: bad operand types for binary operator '/'
System.out.println("Using String:" + (strTest/4));
Hence, you need to convert a String to int before you peform numeric operations on it
int <IntVariableName> = Integer.parseInt(<StringVariableName>);
Pass the string variable as the argument.
This will convert the Java String to java Integer and store it into the specified integer variable.
Check the below code snippet-
class StrConvert{
public static void main(String []args){
String strTest = "100";
int iTest = Integer.parseInt(strTest);
System.out.println("Actual String:"+ strTest);
System.out.println("Converted to Int:" + iTest);
//This will now show some arithmetic operation
System.out.println("Arithmetic Operation on Int: " + (iTest/4));
}
}Output:
Actual String:100 Converted to Int:100 Arithmetic Operation on Int: 25
Integer.valueOf() Method is also used to convert String to Integer in Java.
Following is the code example shows the process of using Integer.valueOf() method:
public class StrConvert{
public static void main(String []args){
String strTest = "100";
//Convert the String to Integer using Integer.valueOf
int iTest = Integer.valueOf(strTest);
System.out.println("Actual String:"+ strTest);
System.out.println("Converted to Int:" + iTest);
//This will now show some arithmetic operation
System.out.println("Arithmetic Operation on Int:" + (iTest/4));
}
}
Output:
Actual String:100 Converted to Int:100 Arithmetic Operation on Int:25
NumberFormatException is thrown If you try to parse an invalid number string. For example, String ‘gtupapers’ cannot be converted into Integer.
Example:
public class StrConvert{
public static void main(String []args){
String strTest = "gtupapers";
int iTest = Integer.valueOf(strTest);
System.out.println("Actual String:"+ strTest);
System.out.println("Converted to Int:" + iTest);
}
}
Above example gives following exception in output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "gtupapers"
Java has had several advanced usage application including working with complex calculations in...
What is OOPS Concept in JavaScript? Many times, variables or arrays are not sufficient to simulate...
Java String endsWith() The Java String endsWith() method is used to check whether the string is...
The String Class Java has three types of Replace methods: replace() replaceAll() replaceFirst() With...
Following is a step by step guide to install Java on Linux. In this training, we will install Java on...
Follow the simple steps below to compile and execute any JAVA program online using your favourite...