Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Convert a String to Int in Java: Methods and Examples

Published on Oct 01,2019 19 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

There are many methods to convert a string to integer in Java. The method most commonly used is the pareseInt() method. There are multiple methods to convert string to an integer. In this article, we will have a look at all the possible methods to convert a String to int in Java

The three main methods are:

  1. parseInt() method
  2. valueOf() method
  3. NumberFormatException method

 

parseInt() method to convert a String to Int in Java

In this method, the string is converted to a primitive int.

Syntax: int dtypename = Integer.parseInt(string);

Example: int res = Integer.parseInt(str);

 

Consider the code:

public class test {
                        public static void main(String args[])
                        {
                                    String str = "102";
                                    int res = Integer.parseInt(str);                                  
                                    System.out.println("the number is: "+ res);
                         }
}

OUTPUT:

Output

EXPLANATION:

In the above code, we have a string variable “102”. We declare another variable res. This is of integer type. So the string variable is converted to an integer by using the parseInt() method.

 

valueOf() method: String to Int in Java

In this method, the string is converted to an Integer object.

Syntax: Integer dtypename = Integer.valueOf(string);

Example: Integer res = Integer.valueOf(str);

 

Consider the code:

public class test {
                        public static void main(String args[])
                        {
                                    String str = "102";
                                    int res = Integer.parseInt(str);                                  
                                    System.out.println("the number is: "+ res);
                         }
}

OUTPUT:

string to int in Java

EXPLANATION:

In the above code, we have a string variable “122”. We declare another variable result as res. This is of integer type. So the string variable is converted to an integer by using the valueOf() method.

NOTE: parseInt() returns a primitive int whereas valueOf() returns a new Integer() object.

 

NumberFormatException Method

A NumberFormatException is thrown when the string is not parsable.

Consider the code:

public class test {
                        public static void main(String args[])
                        {
                                    String str = "102";
                                    int res = Integer.parseInt(str);                                  
                                    System.out.println("the number is: "+ res);
                         }
}

OUTPUT:

string to int in Java

EXPLANATION:

An exception is generated when the string is not parsable.

With this, we come to an end of this String to Int in Java article. I hope you got an idea of all the three methods of Converting a String to int in Java.

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.