How to convert String to Int in Java?

In Java, a number stored in a string can be used to do longest operations in easiest way such as removing duplicates etc. Also String has the capacity to store big integers also. But in some cases it will be useful if you know about type conversion between all data types.

Here in this topic we are going to learn about the conversion of number stored in a string to integer in java. Also, along with that method explanation, there is a final code at end which contains both method conversion. A string can be converted int in Java in following ways.

parseInt(String s)

 The characters in string must be decimal digits excepts character at 0. At 0, it can be ASCII '- or +' to indicate sign of numbers.

public static int parseInt(String s) throws NumberFormatException

Parameters

s - which is of type string containing digits

Returns

Returns the number of Integer type

Exception

Throws NumberFormatException, if the string does not contain parsable Integers

valueOf(String s) 

Returns the integer holding in form of string. It is best to remember valueOf, because it has capability to convert to any data type

public static int valueOf(String s) throws NumberFormatException

Parameters

s - which is of type string containing digits

Returns

Returns the number of Integer type

Exception

Throws NumberFormatException, if the string does not contain parsable Integers

Please let us know your views in comment section

Related Articles

How to convert int to string in Java? 
Visit this page for all Java Conversions 



Comments

Popular posts from this blog

First Unique Character in a String

Balanced Binary Tree

Majority Element

Sequential Digits