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

If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which you can handle:

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

(This treatment defaults a malformed number to 0, but you can do something else if you like.)

Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a 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)
Answer from Rob Hruska on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › convert string to int or integer in java
Convert String to int or Integer in Java | Baeldung
December 15, 2023 - In this article we will show multiple ways of converting a String to an int or Integer.
Discussions

String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
I have also read a couple of resources ... of the string to integer in java. ... The colon is what is giving you the problem. Parse to int is intended to just that - parse a value to an integer (whole numbers). The parser doesn’t know how to handle it. What would 16:45:20 convert ... More on sitepoint.com
🌐 sitepoint.com
0
July 8, 2022
Convert string to int?
As many people are saying, using a count++ would make much more sense. However, if you ever need to do this here is how you would: int num = Integer.parseInt("54321"); More on reddit.com
🌐 r/java
16
0
September 11, 2014
Java - Convert integer to string - Stack Overflow
(See String source in JDK at ...pcode.com/java/root/jdk/openjdk/… .) 2013-11-16T13:36:24.89Z+00:00 ... @jba If you add a zero before an integer, it is taken as an octal nonetheless. So what String.valueof() gives is actually right and is not a pitfall. 2017-04-08T02:11:00.27Z+00:00 ... Returns a String object representing the specified integer. The argument is converted to signed decimal ... More on stackoverflow.com
🌐 stackoverflow.com
Converting Int to String Best Practice
Elie Haykal is having issues with: Hello everyone, What is considered the best way to convert integers to strings. I have been using the +"" but I believe there ar... More on teamtreehouse.com
🌐 teamtreehouse.com
1
August 28, 2015
🌐
Sentry
sentry.io › sentry answers › java › how do i convert a string to an int in java?
How do I convert a String to an int in Java? | Sentry
The two easiest ways to convert a string to an integer in Java are to use Integer.parseInt() or Integer.valueOf().
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › converting.html
Converting Between Numbers and Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string: int i; // Concatenate "i" with an empty string; conversion is handled for you.
🌐
Educative
educative.io › answers › how-to-convert-an-integer-to-a-string-in-java
How to convert an integer to a string in Java
The first argument is any string containing %d. The second argument is the integer you want to convert. This method will replace %d with your integer.
🌐
SitePoint
sitepoint.com › general web dev
String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
July 8, 2022 - I have also read a couple of resources ... of the string to integer in java. ... The colon is what is giving you the problem. Parse to int is intended to just that - parse a value to an integer (whole numbers). The parser doesn’t know how to handle it. What would 16:45:20 convert ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-string-to-int-in-java
String to int in Java - GeeksforGeeks
The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contains non-numeric characters. ... // Java Program to demonstrate // String to int conversion using parseInt() ...
Published   July 23, 2025
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › strings in java › how to convert int to string in java
Convert int to String in Java
December 18, 2024 - We’ll dive deeper into three essential topics that complement the basics of integer-to-string conversion. These additions aim to make your understanding more comprehensive and practical. Standard int types in Java have a maximum value of 2,147,483,647 (Integer.MAX_VALUE). For many applications, this range is sufficient, but what if you need to handle numbers beyond this limit? That’s where long and BigInteger come into play. Here’s how you can handle large integers and convert them to strings:
🌐
Reddit
reddit.com › r/java › convert string to int?
r/java on Reddit: Convert string to int?
September 11, 2014 -

I'm creating a program of a "yes or no" quiz. At the end, the program is supposed to add up the number of yes and nos and give a result that corresponds to the total.

Example: If someone answered yes for all six questions, that would equal '6'. If someone answer yes four times and no twice, that would equal '4'.

I have the program looking pretty good. But I do have one problem that I can't figure out.

When a questioned is asked, the user types in 'yes' or 'no'. I need the yes to convert to 1 and the no to convert to 0 for the program to work.

I need a way to convert the user entry 'yes' or 'no' to '1' and '0', respectively.

Anyone know how to get this to work or point me in the right direction?

🌐
Vultr Docs
docs.vultr.com › java › examples › convert-int-type-variables-to-string
Java Program to convert int type variables to String | Vultr Docs
November 25, 2024 - In this article, you will learn how to convert int type variables to String in Java through several methods and examples.
🌐
Coderanch
coderanch.com › t › 484480 › java › Converting-integer-fixed-length-string
Converting an integer to fixed length string (Java in General forum at Coderanch)
February 24, 2010 - For ex, I have an integer 10, for converting to fixed length string of 8 bytes it would be "00000010".
🌐
YouTube
youtube.com › talented developer
3 Ways to Convert Int to String in Java | Convert Integer to String in Java - YouTube
Convert Integer to String in Java: In this video, you will learn how to convert Int to String in Javausing different ways.video time details :Integer.toStrin...
Published   January 9, 2021
Views   42K
🌐
Team Treehouse
teamtreehouse.com › community › converting-int-to-string-best-practice
Converting Int to String Best Practice (Example) | Treehouse Community
August 28, 2015 - The concatenating way (String = "" + int) is acceptable, but a sort of bad practice and is by some considered bad code smell, but it's easy, especially for someone just starting out.
🌐
TestMu AI Community
community.testmuai.com › ask a question
Convert int to String in Java - TestMu AI Community
February 12, 2025 - How do I convert an int to string in Java? Given a number: int number = 1234; What is the best way to convert this int to string in Java so that the output is: String stringNumber = "1234";
🌐
freeCodeCamp
freecodecamp.org › news › java-string-to-int-how-to-convert-a-string-to-an-integer
Java String to Int – How to Convert a String to an Integer
November 23, 2020 - This leads us to the question – how can we convert a string to an integer? In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer.
🌐
IONOS
ionos.com › digital guide › websites › web development › java int to string
How to convert an int to a string in Java - IONOS
December 18, 2024 - If you want to know how to do the reverse and convert a string to an integer, check out the article in our Digital Guide. Web Hosting Hosting that scales with your ambitions · Stay online with 99.99% uptime and robust security ... One of the simplest and most practical methods for converting a Java int to a string is the method Integer(int).toString.