How do I convert a String to an int in Java?
Explanation
We can convert a String to an int in Java. By using the following statement. And using Integer.parseInt
method.
String string_studyexperts = "1234";
int hoo = Integer.parseInt(string_studyexperts);
And if you know the Java documentation you will notice that the “catch” is that this function can throw a NumberFormatException.
Which of course you have to handle:
int hoo;
try {
hoo = Integer.parseInt(string_studyexperts);
}
catch (NumberFormatException e)
{
hoo = 0;
}
Also, you can do this with the following:
import com.google.common.primitives.Ints;
int hoo = Optional.ofNullable(string_studyexperts)
.map(Ints::tryParse)
.orElse(0)
Also, read How do I delete a Git branch locally and remotely?