TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java Convert String to int

Java Convert String to int example and examples of string to double, int to string, string to date, date to string, string to long, long to string, string to char, char to string, int to long, long to int etc.

<< Back to JAVA

Java Convert String to int

We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which returns instance of Integer class.

Java Convert String to int

Scenario

It is generally used if we have to perform mathematical operations on the string which contains a number. Whenever we receive data from TextField or TextArea, entered data is received as a string. If entered data is in number format, we need to convert the string to an int. To do so, we use Integer.parseInt() method.

Signature

The parseInt() is the static method of Integer class. The signature of parseInt() method is given below:

public static int parseInt(String s)

Java String to int Example: Integer.parseInt()

Let's see the simple code to convert a string to an int in java.

int i=Integer.parseInt("200");

Let's see the simple example of converting String to int in Java.

//Java Program to demonstrate the conversion of String into int
//using Integer.parseInt() method
public class StringToIntExample1{
public static void main(String args[]){
//Declaring String variable
String s="200";
//Converting String into int using Integer.parseInt()
int i=Integer.parseInt(s);
//Printing value of i
System.out.println(i);
}}
Test it Now

Output:

200

Understanding String Concatenation Operator

//Java Program to understand the working of string concatenation operator
public class StringToIntExample{
public static void main(String args[]){
//Declaring String variable
String s="200";
//Converting String into int using Integer.parseInt()
int i=Integer.parseInt(s);
System.out.println(s+100);//200100, because "200"+100, here + is a string concatenation operator
System.out.println(i+100);//300, because 200+100, here + is a binary plus operator
}}
Test it Now

Output:

200100
300

Java String to Integer Example: Integer.valueOf()

The Integer.valueOf() method converts String into Integer object. Let's see the simple code to convert String to Integer in Java.

//Java Program to demonstrate the conversion of String into Integer
//using Integer.valueOf() method
public class StringToIntegerExample2{
public static void main(String args[]){
//Declaring a string
String s="200";
//converting String into Integer using Integer.valueOf() method
Integer i=Integer.valueOf(s);
System.out.println(i);
}}
Test it Now

Output:

300

NumberFormatException Case

If you don't have numbers in string literal, calling Integer.parseInt() or Integer.valueOf() methods throw NumberFormatException.

//Java Program to demonstrate the case of NumberFormatException
public class StringToIntegerExample3{
public static void main(String args[]){
String s="hello";
int i=Integer.parseInt(s);
System.out.println(i);
}}
Test it Now

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "hello"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.parseInt(Integer.java:770)
	at StringToIntegerExample3.main(StringToIntegerExample3.java:4)

References

  1. Integer.parseInt() JavaDoc
  2. Integer.valueOf() JavaDoc
  3. NumberFormatException JavaDoc
Next TopicJava int to String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf