Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, November 24, 2021

Java Convert String To int - Parse Examples

1. Overview

In this tutorial, We'll learn how to convert string to int in java

Let us explore the different ways to convert a string to an integer value using various methods from java built-in API.

This is very common in software life. We need to represent the string that contains numbers into the integer type.

Example:

Input String = "12345";
Output int = 12345;

In Java, we can convert String to int using Integer.parseInt(), Integer.valueOf(), Integer constructor and DecimalFormat.parse(string).intValue() methods and these methods returns integer object.

Java Examples - Convert String To int


2. String to Int: Using Integer.parseInt()


parseInt(String string) method is static method and it is present in Integer class.This method converts the given string into integer number. If the given string contains at least non digit then it will throw the runtime error NumberFormatException.

public class ParseIntExample 
{
    public static void main(String[] args) 
    {
		// Creating a string
        String s = "12345";
		
		// converting string to int using parseInt() method
        int i = Integer.parseInt(s);
		
		// printing the input and output
        System.out.println("string : "+s);
        System.out.println("integer : "+i);
    }
}

Output:

string : 12345
integer : 12345

3. String to Int: Using Integer.valueOf()


Next, use valueOf(String s) method to convert String object to an int value. But this method also works as similar to the parseInt() method.

But the main difference between parseInt() and valueOf() method is parseInt() method returns primitive int and valueOf() method returns wrapper Integer object.

public class ValueOfExample
{
    // Example to string to int
    public static void main(String[] args) 
    {
		// Creating String object with value "1000"
        String s2 = "1000";
		
		// Calling valueOf() method which does string to int conversion
        Integer i2 = Integer.valueOf(s2);
		
		// printing values
        System.out.println("string value: "+s2);
        System.out.println("integer value: "+i2);
    }
}

Output:

string value: 1000
integer value: 1000

4. String to Int: Using Integer Constructor with String Argument


Next, Look at another approach which does use constructor of Integer class with string type as an argument as new Integer("2468");

public class IntegerConstructorExample
{
	// string to int with integer constructor
    public static void main(String[] args) 
    {
		// creating string object 3
        String s3 = "999";
		
		// convert string to integer with Interger constructor
        Integer i3 = new Integer(s3);
		
         // convert Wrapper integer to primitive int using primitive int
        int int3 = i3.intValue();
		
		// printing the values
        System.out.println("String s3 value : " + int3);
        System.out.println("Integer int3 value : " + int3);
    }
}

Output:

String s3 value : 999
Integer int3 value : 999

5. String to Int: Using DecimalFormat class


The final approach is, Use DecimalFormat.parse() method with string as an argument. Pass value "0" to DecimalFormat class constructor so that it treats only the numbers as input.

Next, call parse() and pass the string value to be converted into an int value. parse() method returns Number object and needs to get the int value from intValue() method from Number class.

Example:

import java.text.DecimalFormat;
import java.text.ParseException;

public class DecimalFormatExample 
{
    // DecimalFormat.parse() example to parse string into int value.
    public static void main(String[] args) throws ParseException 
    {
		// Creating a string object with value 123456
        String string = "123456";

        // Passing "0" to the DecimalFormat indicates that it shuould accepts only digits.
        DecimalFormat decimalFormat = new DecimalFormat("0");

        // parsing String to Number
        Number number = decimalFormat.parse(string);
     
        // converting Number to primitive int.
        int i4 = number.intValue();

		// printing values
       System.out.println("string value : " + string);		
       System.out.println("i4 value : " + i4);
    }
}

Output:

string value : 123456
i4 value : 123456

6. String To Int Exception Java


If the input string literal is having the non numeric characters and call Integer class parseInt() or valueOf() or constructor or DecimalFormat class methods will end up in runtime exception "NumberFormatException".

package com.javaprogramto.convert.string.toint;

/**
 * Example to exception in string to integer conversion
 * 
 * @author Javaprogramto.com
 *
 */
public class StringToIntException {

	public static void main(String[] args) {

		// creating string with alphabets
		String s = "hello world";

		// convert string to int using parseInt() method. This will throw exception
		// int number = Integer.parseInt(s);
		// int number = Integer.valueOf(s);
		Integer number = new Integer(s);

		// printing vlaues
		System.out.println("Number : " + number);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hello world"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
	at java.base/java.lang.Integer.parseInt(Integer.java:658)
	at java.base/java.lang.Integer.<init>(Integer.java:1117)
	at com.javaprogramto.convert.string.toint.StringToIntException.main(StringToIntException.java:19)



7. Conclusion


In this article, We've seen how to convert string to integer in different ways in java with example programs.


No comments:

Post a Comment

Please do not add any spam links in the comments section.