How do I convert a String to an int in Java

0 votes
My String contains only numbers, and I want to return the number it represents.

For example, given the string "1234" the result should be the number 1234.

How can I convert a String to an int in Java?
Jul 28, 2020 in Java by kartik
• 37,510 points
363 views

2 answers to this question.

0 votes

Hello @kartik,

We can use the parseInt(String str) method of the Integer wrapper class for converting a String value to an integer value.

For example:

String strValue = "12345";
Integer intValue = Integer.parseInt(strVal);

The Integer class also provides the valueOf(String str) method:

String strValue = "12345";
Integer intValue = Integer.valueOf(strValue);

We can also use toInt(String strValue) of NumberUtils Utility Class for the conversion:

String strValue = "12345";
Integer intValue = NumberUtils.toInt(strValue);

Hope it helps!!
Thank you!

answered Jul 28, 2020 by Niroj
• 82,680 points
0 votes
Use the lines of code mentioned below:-

String myString = "1234"; 
int foo = Integer.parseInt(myString);

The documentation of Java mentions that the "catch" is when this function can throw a NumberFormatException, which you can handle by using the code mentioned below:
 

int foo; 
try { 
    foo = Integer.parseInt(myString); 
} 
catch (NumberFormatException e) { 
  foo = 0; 
}

This will ensure that they default a malformed number to 0, but you could try another way if wished upon. However, you can also use an Ints method from the Guava library, which is in combination with Java 8's Optional and hence, this makes a more powerful and concise way to convert a string into an int:
 

import com.google.common.primitives.Ints; 

int foo = Optional.ofNullable(myString) 
.map(Ints::tryParse) 
.orElse(0)
answered Feb 9 by Soham
• 8,730 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,170 points
1,013 views
0 votes
2 answers
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,170 points
1,587 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,900 points
14,517 views
0 votes
1 answer

How to avoid Java code in JSP files?

Hello @kartik, JSTL offers tags for conditionals, loops, sets, ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,680 points
167 views
0 votes
1 answer

How to directly initialize a HashMap?

Hello @Kartik, In plain java 8 you also ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,680 points
374 views
0 votes
1 answer

How can I get the current stack trace in Java?

Hello @kartik, Try this: Thread.currentThread().getStackTrace(); is fine if you don't ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,680 points
220 views
0 votes
1 answer

How to use an existing database with an Android application?

Hello @kartik, If you are having pre built ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,680 points
854 views
0 votes
1 answer

How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,680 points
1,022 views
0 votes
1 answer

How do I make a list with checkboxes in Java Swing?

Hii @kartik, Create a custom ListCellRenderer and asign it to ...READ MORE

answered May 8, 2020 in Java by Niroj
• 82,680 points
1,173 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP