Convert String To Integer in Java

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Converting a String to an Integer is a common operation in Java, especially when dealing with user input or reading data from files. In this blog post, we will explore various methods to convert a String to an Integer and provide practical examples with outputs to illustrate each approach. 

Method 1: Using Integer.parseInt() Method 

The Integer.parseInt() method is a straightforward way to convert a String to an Integer. It parses the string argument as a signed decimal integer and returns the resulting numeric value. 

Example:

public class StringToIntegerExample { public static void main(String[] args) { String strNumber = "42"; try { // Convert String to Integer using parseInt() int intValue = Integer.parseInt(strNumber); System.out.println("Input String: " + strNumber); System.out.println("Integer Value: " + intValue); } catch (NumberFormatException e) { System.out.println("Error: Invalid input for integer conversion"); } } } 

Output:

Input String: 42 Integer Value: 42

Method 2: Using Integer.valueOf() Method 

The Integer.valueOf() method converts a String to an Integer object. It returns an Integer instance representing the specified int value if the String is a valid representation of an integer. 

Example:

public class StringToIntegerExample { public static void main(String[] args) { String strNumber = "100"; try { // Convert String to Integer using valueOf() Integer integerValue = Integer.valueOf(strNumber); System.out.println("Input String: " + strNumber); System.out.println("Integer Value: " + integerValue); } catch (NumberFormatException e) { System.out.println("Error: Invalid input for integer conversion"); } } } 

Output:

Input String: 100 Integer Value: 100

Method 3: Using NumberFormat.parse() Method 

You can also use NumberFormat to parse the String and convert it to an Integer. 

Example:

import java.text.NumberFormat; import java.text.ParseException; public class StringToIntegerExample { public static void main(String[] args) { String strNumber = "2022"; // Convert String to Integer using NumberFormat try { NumberFormat numberFormat = NumberFormat.getInstance(); int intValue = numberFormat.parse(strNumber).intValue(); System.out.println("Input String: " + strNumber); System.out.println("Integer Value: " + intValue); } catch (ParseException e) { System.out.println("Error: Invalid input for integer conversion"); } } } 

Output:

Input String: 2022 Integer Value: 2022

Method 4: Using Scanner class 

The Scanner class can also be used to convert a String to an Integer. 

Example:

import java.util.Scanner; public class StringToIntegerExample { public static void main(String[] args) { String strNumber = "999"; // Convert String to Integer using Scanner try { Scanner scanner = new Scanner(strNumber); int intValue = scanner.nextInt(); System.out.println("Input String: " + strNumber); System.out.println("Integer Value: " + intValue); } catch (NumberFormatException e) { System.out.println("Error: Invalid input for integer conversion"); } } } 

Output:

Input String: 999 Integer Value: 999

Conclusion

Converting a String to an Integer is a fundamental operation in Java. You can use methods like Integer.parseInt(), Integer.valueOf(), NumberFormat or Scanner to achieve this. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Remember to handle exceptions appropriately to gracefully handle invalid input and ensure the reliability of your code.

Related String Conversion Examples

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare