Java Convert String to Int

What we are going to learn ?

  • Parsing methods in Java Convert String to Int
  • [New] Using Java 8, Convert set of String to set of Int

parsing

Parsing Methods in Java Convert String to Int

In Java there are two methods to convert String to Int, they are.

  1. Integer.parseInt()
  2. Integer.valueOf()

1. Convert String to Int using Integer.parseInt()

The Integer.parseInt() is a static method of Integer class used in java to converts string to primitive integer.

Example

String ‘100’ to primitive integer 100

Note:Parsing is a process of converting one data type to another. For example String to integer.

Coding                 

In this example we are converting string to integer value using Integer.parseInt() method involves following steps like,

1. Inside void main initialize both string value and integer value (as shown in the below program).

2. Then pass the string variable as the argument. using Integer.parseInt() method convert the String to Integer and store it in the integer variable called “num2”.

3. Finally perform Arithmetic operations like addition and store the result in “sum”, display the result.

package stringToint;

public class StringToint {

public static void main(String[] args) {

String str=”123″; //String value

int num1 = 100; //integer value

int num2 = Integer.parseInt(str); //converting the string to an integer value stored in num2

int sum=num1+num2; //performing Arithmetic operations

System.out.println(“Result is: “+sum); //Display the result

}

}

Output: 223

Screenshot     

strinToint using parseInt

2. Convert String to Int using Integer.valueOf()

This is a method in java used to converting string to an integer object.

Coding

In this example we are converting string to integer value using Integer.valueOf() method involves following steps like,

1. Inside void main initialize both string value and integer value (as shown in the below program).

2. Then pass the string variable as the argument. Using Integer.valueOf() method convert the String to Integer and store it in the integer variable called “num2”.

3. Finally perform Arithmetic operations like addition and store the result in “sum”, display the result.

package stringToint;

public class StringToint {

public static void main(String[] args) {

String str=”123″; //string value

int num1 = 110; //integer value

//converting the string to an integer value stored in num2

int num2 = Integer.valueOf(str);

int sum=num1+num2; //performing Arithmetic operations

System.out.println(“Result is: “+sum);  //Display the Result

}

}

Output:233

Screenshot

strinToint using valueof

Note: The main difference between the two methods are, parseInt(String) returns a primitive int and valueOf(String) returns a new Integer() object.

3. NumberFormatException

The NumberFormatException is thrown when we try to parse an invalid number string. For example, String ‘beyondcorner18’ cannot be converted into Integer.

Coding

package stringToint;

public class StringToint {

public static void main(String[] args) {

String number = “beyondcorner18”; //invalid number string

int result = Integer.parseInt(number); //parsing step

System.out.println(result); //Display the result

}

}

Output: Exception in thread “main” java.lang.NumberFormatException: For input string: “beyondcorner18”

Screenshot

NumberformatException

Java 8 – Convert String to Int

In Java 8 using stream API  we can convert set of String to set of Integer, it involves following steps like,

1. The Set.Stream() is used to convert set<string> to stream<string>.

2. The Steam.map() is used to convert stream<string> to stream<integer>.

3. The Collectorts.toset() is used to Accumulate stream<integer> to set<integer>.

4. The map() operation is used to throw NumberFormatException, if string does not contains parsable integer.

Coding

package stringToint;

import java.util.Arrays;

import java.util.Set;

import java.util.HashSet;

import java.util.stream.Collectors;

import java.awt.List;

public class StringToint {

public static void main(String[] args) {

Set<String> set = new HashSet<>(Arrays.asList(“-2” , “2”, “-3”));

Set<Integer> ints = set.stream()

.map(s -> Integer.parseInt(s))

.collect(Collectors.toSet());

System.out.println(ints);

}

}

Output:[-2, 2, -3]

Screenshot

Stringtoint in java 8

That’s all about use of Java Convert String to Int, Integer.parseInt() and Integer.valueOf() methods, feel free to post your comments.

2 Comments

Leave a Reply

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