Katerina Zamani

About Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java convert string to int Example

String to Integer conversion is a frequently used procedure because String and Integer are very common data types.

In this example we are going to show two different ways of converting String to Integer in Java, by using parseInt() and valueOf() methods.

 
 
 
 
 

1. Syntax of parseInt()

There are two different expressions for parseInt() method:

The parameters are:

  • s: is the string, that includes the integer representation we want to parse.
  • radix: is optional and it represents the base of the used numbering system, while parsing s.

Both expressions return the integer value represented by the string argument. If radix parameter is used, the returned value is expressed by the specified radix. Otherwise the default radix is set to 10, for decimal system.

2. Syntax of valueOf()

valueOf() method, which is utilized for string to int conversion too, can be expressed in two ways:

The parameters of valueOf() method are:

  • s: represents the parsing string.
  • radix: is the radix that is used in the parsing string. This parameter is optional.

The returned value of valueOf() method is an Integer object that represents the string argument. If the radix argument is used, the representation will be expressed by the specific radix. Otherwise, the value is a signed decimal integer.

3. Example of String to Integer conversion

Create a java class with the name StringToIntTest.java and paste the following code.

StringToIntTest.java:

package com.javacodegeeks.javabasics.stringtoint;

public class StringToIntTest {
	
	public static void main(String args[]) {

		String testString1 = "123456789";
		String testString2 = "00123";
		String errorString = "0.0123";
		
		try{
			// using Integer.parseInt
			// with radix
			int int1 = Integer.parseInt(testString1, 10);
			// default radix = 10
			int int2 = Integer.parseInt(testString2);
			System.out.println("With parseInt method, int1 = " + int1 + " and int2 = "+int2);
			
			// error situation
			int interror = Integer.parseInt(errorString);
			System.out.println("With parseInt method, interror =" + interror);
		}catch(NumberFormatException ex) {
			System.err.println("NumberFormatException in parseInt, "+ ex.getMessage());
		}
		
		try{
			// using Integer.valueOf without radix
			int int1 = Integer.valueOf(testString1);
			int int2 = Integer.valueOf(testString2);
			System.out.println("With valueOf method, int1 = " + int1 + " and int2 = " + int2);
			
			// error situation
			int interror = Integer.valueOf(errorString);
			System.out.println("With valueOf method, interror =" + interror);
		}catch(NumberFormatException ex) {
			System.err.println("NumberFormatException in valueOf, "+ ex.getMessage());
		}

	}

}

In the code above, we use both ways in order to convert a string to int and we take some different conditions. Notice that testString2 string contains two leading zeros. As you can see in the output below, both methods will ignore these zeros at the beginning and will display the integer. Also notice that we use NumberFormatException if the string cannot be parsed as an integer.

You can see the output of the execution below.

Output:


With parseInt method, int1 = 123456789 and int2 = 123
NumberFormatException in parseInt, For input string: "0.0123"
With valueOf method, int1 = 123456789 and int2 = 123
NumberFormatException in valueOf, For input string: "0.0123"

As you can see it throws a NumberFormatException for the errorString, as it couldn’t be parsed to integer. Also, the leading zeros are ignored, as we said before.

Download the source code

This was an example of string to int conversion in Java. Download the source code of this example: StringToIntTest.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close