How to Convert String into Integer in Java:
1. Using Integer.parseInt() Method. |
2. Using Integer.valueOf() Method. |
3. tryParse Method Of Guava Library. |
4. NumberFormatException Case. |
5. Leading zeros Case. |
We can convert String into Integer Using three methods namely Integer.parseInt(), Integer.ValueOf(), and the tryParse Methods.
The Conversion of String to Integer is used in Java when we want to do the Mathematical operations.
Generally, the input is taken in the form of a string in java.
If the input string is number then in such cases there should be a method to convert String into Integer.
So in this lesson, we will see How to Convert String into Integer in Java using different Methods with Explanation one by one.
Table of Contents
1. How to Convert String into Integer in Java Using Integer.parseInt() Method
In this program, we will make use of java inbuilt method called as Integer.parseInt() method. So, first of all
What is Integer.parseInt() Method?
Generally, Integer.parseInt() is a java inbuilt method which is used to convert the input string in the form of an integer.
Syntax: Integer.parseInt()
For Example: str=”12345″; int j=Integer.parseInt(str);
After executing the example line in the program we will get the output as 12345.
If the Input String is not valid then the program throws the Exception called NumberFormatException.
So let us see how to write a program to convert string to integer in java using Integer.parseInt() method.
//Learnprogramo public class Main { public static void main(String args[]) { //Declaring String variable String s="575"; //Converting String into int using Integer.parseInt() int j=Integer.parseInt(s); //Printing value of i System.out.println(j); } }
Output:
In the above program, we have given input as 575 and store it in String variable s.
Integer.parseInt(s) converts the string into an Integer Hence we get the output as Integer i.e 575.
2. How to Convert String into Integer in Java Using Integer.valueOf() Method
So, before start learning, we will do a quick overview of inbuilt java method called as Integer.valueOf(). So
What is Integer.valueOf() Method in Java?
This inbuilt java method is used to converting the string value in the integer.
This method returns the integer object which holds the value in the form of string.
Syntax: Integer.valueOf(s).
For Example: String s=”12345″; int j=Integer.valueOf(s);
In the above example, we have given input string as 12345 and stored it in s.
By using method Integer.valueOf(s) the string value 12345 is converted into an integer.
//Learnprogramo public class Main { public static void main(String args[]) { //Declaring a string String s="12345"; //converting String into Integer using Integer.valueOf() method Integer j=Integer.valueOf(s); System.out.println(j); } }
Output:
3. Using Ints::tryParse Method of Guava Library
This method is of Guava Library which is similar to Integer.parseInt() method and is more powerful than other methods.
So let us see the conversion of a string into an integer in java using tryParse method of Guava Library.
//Learnprogramo import java.io.*; import java.util.*; import com.google.common.primitives.Ints; public class Main { // Function to convert String to integer public static int convert(String str) { int val = 0; System.out.println("String = " + str); // Convert the String val = Optional.ofNullable(str) .map(Ints::tryParse) .orElse(0); return val; } //main public static void main(String[] args) { String str = "1234"; int val = convert(str); System.out.println("Integer value = " + val); System.out.println(); str = "123s"; val = convert(str); System.out.println("Integer value = " + val); } }
Output:
4. NumberFormatException
The NumberFormatException is a type of Exception in java which mainly arises when the input string is invalid.
For Example “programo123” cannot be converted into the integer because the input string is invalid which contains string as well as integers.
So let us see how the NumberFormatException arises in java during the execution of the program.
//Learnprogramo public class Main { public static void main(String args[]) { String s="programo123"; int j=Integer.parseInt(s); System.out.println(j); } }
Output:
5. How to Convert String into Integer in Java with leading zeros
consider a string which contains all the integer values with leading zeros. So to perform an arithmetic operation on that string which retains the leading zeros we will need to first convert the string into an integer.
After we will use format() method to convert the output value into the string.
//Learnprogramo public class Main { public static void main(String args[]) { String str="00000575"; /* String to int conversion with leading zeroes * the %08 format specifier is used to have 8 digits in * the number, this ensures the leading zeroes */ str = String.format("%08d", Integer.parseInt(str)+102); System.out.println("Output String: "+str); } }
Output:
Also Read:
- How to run java program in cmd.
- Different Star Pattern Program in Java.
- Number Pattern Program in java.
- Program to reverse a String in Java.
- How to Convert Int to String in Java.