A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://howtodoinjava.com/java/string/convert-string-to-int/ below:

Convert String to int in Java (with Examples)

Learn to convert a Java String to int value. Note that string can contain a normal decimal value or a different value in other bases or radix.

1. Using Integer.parseInt() 1.1. Syntax

The Integer.parseInt() parses a String to a signed int value. This method is an overloaded method with the following arguments:

public static int parseInt(string) throws NumberFormatException { ... }
public static int parseInt(string, int radix) throws NumberFormatException { ... }
parseInt(string, int beginIndex, int endIndex, int radix) throws NumberFormatException { ... }
1.2. Example

In the following Java program, we are parsing the string “1001” using all three above-mentioned methods and verifying that output.

Assertions.assertEquals(1001, Integer.parseInt("1001"));
Assertions.assertEquals(513, Integer.parseInt("1001", 8));
Assertions.assertEquals(1001, Integer.parseInt("amount=1001", 7, 11, 10));
1.3. Throws NumberFormatException

All three methods throw NumberFormatException if:

Assertions.assertThrows(NumberFormatException.class, ()->{
  Integer.parseInt(null);
});

Assertions.assertThrows(NumberFormatException.class, ()->{
    Integer.parseInt("abc");
});
2. Using Integer.valueOf()

The Integer.valueOf() is very similar to parseInt() method – with only one difference that return type is Integer type instead of primitive int. In fact, if we look at the source code of valueOf() method, it internally calls parseInt() method.

It is also overloaded in two forms. Notice the method return type is Integer.

public static Integer valueOf(String s) throws NumberFormatException {...}
public static Integer valueOf(String s, int radix) throws NumberFormatException {...}

Both methods throw the NumberFormatException in all cases, similar to parseInt().

Assertions.assertEquals(1001, Integer.valueOf("1001"));
Assertions.assertEquals(513, Integer.valueOf("1001", 8));

Assertions.assertThrows(NumberFormatException.class, ()->{
  Integer.valueOf("abc");
});
3. Using Integer.decode()

The Integer.decode() is another useful method for String to int conversion but only for decimal, hexadecimal and octal numbers. Note that:

public static Integer decode(String s) throws NumberFormatException

Let us see an example to understand it better.

Assertions.assertEquals(100, Integer.decode("+100"));
Assertions.assertEquals(64, Integer.decode("+0100"));
Assertions.assertEquals(256, Integer.decode("+0x100"));
4. Conclusion

In this quick Java tutorial, we learned to parse a string to int or Integer type. We also learned to parse the numbers in decimal, octal and hexadecimal formats.

Happy Learning !!

Sourcecode on Github


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4