Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
String to int in Java
Next article icon

String to int in Java

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting a String to an int in Java can be done using methods provided in the Integer class, such as Integer.parseInt() or Integer.valueOf() methods.

Example:

The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contains non-numeric characters.

Java
// Java Program to demonstrate  // String to int conversion using parseInt() public class StringToInt {     public static void main(String[] args) {                String s = "12345";           // Convert the string to an integer          // using Integer.parseInt()         int n = Integer.parseInt(s);          System.out.println("" + n);     } } 

Output
12345 

Other Ways to Convert String to int

Using Integer.valueOf() Method

The Integer.valueOf() method converts a String to an Integer object instead of a primitive int. We can unbox it to an int.

Note: valueOf() method uses parseInt() internally to convert to integer. 

Example:

Java
// Java ProgramConvert String to int  // using Integer.valueOf() Method public class StringToInt {         public static void main(String[] args) {                // Convert String to Integer using valueOf()         String s = "217";                // Convert the string to an Integer object          // using Integer.valueOf()         int n = Integer.valueOf(s);                  System.out.println("" + n);     } } 

Output
217 


Handling Errors for Non-Numeric Strings

If a string literal does not contain numeric values, calling the Integer.parseInt() or Integer.valueOf() methods will result in a NumberFormatException.

Example:

Java
// Java Program to Demonstrate Working of parseInt() Method // where NumberFormatException is Thrown public class StringToIntExample {      public static void main(String[] args) {                  // NumberFormatException due to empty string         String s1 = "";         int n1 = Integer.parseInt(s1);                   // NumberFormatException due to non-numeric string         String s2 = "geeksforgeeks";         int n2 = Integer.parseInt(s2);           System.out.println(n1);         System.out.println(n2);     } } 

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" 	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 	at java.base/java.lang.Integer.parseInt(Integer.java:662) 	at java.base/java.lang.Integer.parseInt(Integer.java:770) 	at StringToIntExample.main(StringToIntExample.java:10)

Next Article
String to int in Java

K

kartik
Improve
Article Tags :
  • Java
  • Computer Science Fundamentals
  • Java-Strings
  • Java-lang package
  • Java-Functions
  • Java-Integer
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

    Integer toString() in Java
    The java.lang.Integer.toString() is an inbuilt method in Java which is used to return the String object representing this Integer's value. Syntax : public static String toString() Parameters: The method does not accept any parameters. Return Value:The method returns the string object of the particul
    5 min read
    String Class in Java
    A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
    7 min read
    How to Convert a String to an int in Java?
    In Java, converting a String to an int is done using methods from the Integer class. The methods used for this conversion are Integer.parseInt() and Integer.valueOf(). Example:The Integer.parseInt() is a commonly used method to convert a string to an integer in Java. This method converts a numeric s
    1 min read
    Java Strings
    In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
    9 min read
    Integer toOctalString() Method in Java
    Octal is the base-8 number system and uses the digits 0 to 7 in operation. Octal is widely used in computing on systems like the PDP-8, and IBM mainframes employed 12-bit, 24-bit or 36-bit words. The Java.lang.Integer.toOctalString() method is a built-in function in Java that is used to return a str
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences