BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String to int Conversion

Last Updated: June 10, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to convert a String to int in Java. If a String is made up of digits like 1,2,3 etc, any arithmetic operation cannot be performed on it until it gets converted into an integer value. In this tutorial we will see the following two ways to convert String to int:

  • Using Integer.parseInt()
  • Using Integer.valueOf()

1. Using Integer.parseInt()

The Integer.parseInt() method converts a String to a primitive int. I have covered this method in detail here: Integer.parseInt() Method

String number = "123";
int result = Integer.parseInt(number);
System.out.println(result); // Output: 123

2. Using Integer.valueOf()

The Integer.valueOf() method converts a String to an Integer object, which can then be unboxed to a primitive int.

String number = "123";
int result = Integer.valueOf(number);
System.out.println(result); // Output: 123

3. Handling Exceptions

Both of these methods throw NumberFormatException, if the string cannot be parsed as an int. It is always a good practice to place the conversion code inside try block and handle this exception in catch block to avoid unintentional termination of the program.

String number = "123abc"; // This will cause an exception
try {
int result = Integer.parseInt(number);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("This value cannot be parsed as an integer");
}

String to int Conversion Example

Let’s write a complete program for string to int conversion using both the methods that we discussed above. We are also handling exception, if in case the string cannot be parsed as an integer.

public class StringToIntExample {
public static void main(String[] args) {
String validNumber = "123";
String invalidNumber = "123a";

// Using Integer.parseInt() method
try {
int result = Integer.parseInt(validNumber);
System.out.println("Using Integer.parseInt(): " + result);
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " + validNumber);
}

// Using Integer.valueOf() method
try {
int result = Integer.valueOf(validNumber);
System.out.println("Using Integer.valueOf(): " + result);
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " + validNumber);
}

// Handling NumberFormatException for invalid input
try {
int result = Integer.parseInt(invalidNumber);
System.out.println("Using Integer.parseInt(): " + result);
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " + invalidNumber);
}
}
}

Output:

Using Integer.parseInt(): 123
Using Integer.valueOf(): 123
Invalid number format: 123a

Recommended Posts

  • Java int to String Conversion
  • Java String to long Conversion
  • Java String to double Conversion
  • Java String to char Conversion
  • Java String to Object Conversion
  • Java String to float Conversion
  • Java String to Date Conversion
  • Java String to boolean Conversion

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap