Last Updated On By Anmol Lohana
Sometimes while we are working with strings, we need to convert that string value into an integer value. Suppose we have a number in string form, and now we want to perform a mathematical operation on it. While it is a string, we cannot perform any mathematical operation on it, and if we try to do that, it will throw an error. To do this task, we need to convert a string into an integer first, and then we can perform any operation we want. There are different ways to convert a string to an integer in Java.
Table of Contents
The parseInt() Method belongs to the integer class of the Java.lang package. It converts a string to a primitive int value. There are three types of parseInt() Method and that are:
Let’s look at an example in which we will use three of these methods of parseInt() Method.
public class IntegerParseInt {
public static void main(String[] args) {
String string1 = "100";
String string2 = "100";
String string3 = "Google";
String string4 = "20";
int number1 = Integer.parseInt(string1);
int number2 = Integer.parseInt(string2, 16);
int number3 = Integer.parseInt(string3, 32);
int number4 = Integer.parseInt(string4, 8);
System.out.println("Parsing String \"" + string1 + "\": " + number1);
System.out.println("Parsing String \"" + string2 + "\" in base 16: " + number2);
System.out.println("Parsing String \"" + string3 + "\" in base 32: " + number3);
System.out.println("Parsing String \"" + string4 + "\" in base 8: " + number4);
}}
The valueOf() method takes the input value and convert/parse it into an integer. It also has three types:
Let’s have a look on an example.
public class ValueOfMethod{
public static void main(String args[]){
int i = 10;
String string1 = "100";
String string2 = "100";
String string3 = "Google";
String string4 = "20";
int number1 = Integer.valueOf(i);
int number2 = Integer.valueOf(string1);
int number3 = Integer.valueOf(string3, 32);
int number4 = Integer.valueOf(string4, 8);
System.out.println("Parsing int " + i + ": " + number1);
System.out.println("Parsing String \"" + string1 + "\": " + number2);
System.out.println("Parsing String \"" + string3 + "\" in base 32: " + number3);
System.out.println("Parsing String \"" + string4 + "\" in base 8: " + number4);
}}
The decode() Method accepts only one parameter, and it has one type that is decode(String s). It accepts a string and decodes it into an integer.
public class DecodeMethod{
public static void main(String args[]){
String string1 = "100";
String string2 = "50";
String string3 = "20";
int number1 = Integer.decode(string1);
int number2 = Integer.decode(string2);
int number3 = Integer.decode(string3);
System.out.println("Parsing String \"" + string1 + "\": " + number2);
System.out.println("Parsing String \"" + string2 + "\": " + number2);
System.out.println("Parsing String \"" + string3 + "\": " + number3);
}}
In conclusion, we discussed the conversion string to integer in java programming. We saw three different methods to perform this task with their coding example to understand it more clearly. The methods we discussed were parseInt(), valueOf(), and decode() Method; we used these three methods to convert the string value into an int value.