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 of course you have to 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:


int foo = Optional.ofNullable(myString)

How do I convert a String to an int in Java? - Stack Overflow

String mystr = mystr.replaceAll("[^\\d]", ""); int number = Integer.parseInt( ...

Java Convert String to int - javatpoint

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 ...

Java Convert String to int examples

String str="-1122"; int inum = Integer.valueOf(str);. Value of inum would be -1122. Similar to the parseInt(String) ...

How to convert an integer to a string in Java

Common ways to convert an integer · 1. The toString() method. This method is present in many Java classes. It returns a string. · 2. String.valueOf() · 3.

Integer (Java Platform SE 7 )

Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. Method Summary. Methods. Modifier and Type, Method ...

JAVAstringint 互相转化| 菜鸟教程

2、 int i = Integer.valueOf(my_str).intValue();. 注: 字串转成Double, Float, Long 的方法大同小异. 2 如何将整数int 转换成字串String ?