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 OverflowString 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)
For example, here are two ways:
Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
There is a slight difference between these methods:
valueOfreturns a new or cached instance ofjava.lang.IntegerparseIntreturns primitiveint.
The same is for all cases: Short.valueOf/parseShort, Long.valueOf/parseLong, etc.
String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
Convert string to int?
Java - Convert integer to string - Stack Overflow
Converting Int to String Best Practice
Videos
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?
There are multiple ways:
String.valueOf(number)(my preference)"" + number(I don't know how the compiler handles it, perhaps it is as efficient as the above)Integer.toString(number)
Integer class has static method toString() - you can use it:
int i = 1234;
String str = Integer.toString(i);
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.