Contents
1. Introduction
There are several ways of converting a String to an integer in Java. We present a few of them here with a discussion of the pros and cons of each.
2. Integer.parseInt()
Integer.parseInt(String) can parse a string and return a plain int value. The string value is parsed assuming a radix of 10
. The string can contain “+
” and “-
” characters at the start to indicate a positive or negative number.
int value = Integer.parseInt("25"); // returns 25 int value = Integer.parseInt("-43"); // return -43 int value = Integer.parseInt("+9061"); // returns 9061
Illegal characters within the string (including period “.
“) result in a NumberFormatException. Additionally a string containing a number larger than Integer.MAX_VALUE (231 - 1
) also result in a NumberFormatException.
// throws NumberFormatException -- contains "." int value = Integer.parseInt("25.0"); // throws NumberFormatException -- contains text int value = Integer.parseInt("93hello"); // throws NumberFormatException -- too large int value = Integer.parseInt("2367423890");
To explicitly specify the radix, use Integer.parseInt(String,int) and pass the radix as the second argument.
// returns 443 int value = Integer.parseInt("673", 8); // throws NumberFormatException -- contains character "9" not valid for radix 8 int value = Integer.parseInt("9061", 8); // returns 70966758 -- "h" and "i" are valid characters for radix 20. int value = Integer.parseInt("123aghi", 20);
3. Integer.valueOf()
The static method Integer.valueOf() works similar to Integer.parseInt() with the difference that the method returns an Integer object rather than an int value.
// returns 733 Integer value = Integer.valueOf("733");
Use this method when you need an Integer object rather than a bare int. This method invokes Integer.parseInt(String) and creates an Integer from the result.
4. Integer.decode()
For parsing an integer starting with these prefixes: “0” for octal, “0x”, “0X” and “#” for hex, you can use the method Integer.decode(String). An optional “+” or “-” sign can precede the number. Al the following formats are supported by this method:
Signopt DecimalNumeral Signopt 0x HexDigits Signopt 0X HexDigits Signopt # HexDigits Signopt 0 OctalDigits
As with Integer.valueOf(), this method returns an Integer object rather than a plain int. Some examples follow:
// returns 53 Integer value = Integer.decode("0x35"); // returns 1194684 Integer value = Integer.decode("#123abc'); // throws NumberFormatException -- value too large Integer value = Integer.decode("#123abcdef"); // returns -231 Integer value = Integer.decode("-0347");
5. Convert Large Values into Long
When the value being parsed does not fit in an integer (231-1
), a NumberFormatException is thrown. In these cases, you can use analogous methods of the Long class: Long.parseLong(String), Long.valueOf(String) and Long.decode(String). These work similar to their Integer counterparts but return a Long object (or a long in the case of Long.parseLong(String)). The limit of a long is Long.LONG_MAX (defined to be 263-1
).
// returns 378943640350 long value = Long.parseLong("378943640350"); // returns 3935157603823 Long value = Long.decode("0x39439abcdef");
6. Use BigInteger for Larger Numbers
Java provides another numeric type: java.math.BigInteger with an arbitrary precision. A disadvantage of using BigInteger is that common operations like adding, subtracting, etc require method invocation and cannot be used with operators like “+
“, “-
“, etc.
To convert a String to a BigInteger, just use the constructor as follows:
BigInteger bi = new BigInteger("3489534895489358943");
To specify a radix different than 10, use a different constructor:
BigInteger bi = new BigInteger("324789045345498589", 12);
7. Parse for Numbers Within Text
To parse for numbers interspersed with arbitrary text, you can use the java.util.Scanner class as follows:
String str = "hello123: we have a 1000 worlds out there."; Scanner scanner = new Scanner(str).useDemiliter("\\D+"); while (s.hasNextInt()) System.out.printf("(%1$d) ", s.nextInt()); // prints "(123) (1000)"
This method offers a powerful way of parsing for numbers, although it comes with the expense of using a regular expression scanner.
Summary
To summarize, there are various methods of converting a string to an int in java.
- Integer.parseInt() is the simplest and returns an int.
- Integer.valueOf() is similar but returns an Integer object.
- Integer.decode() can parse numbers starting with “
0x
” and “0
” as hex and octal respectively. - For larger numbers, use the corresponding methods in the Long class.
- And for arbitrary precision integers, use the BigInteger class.
- Finally, to parse arbitrary text for numbers, we can use the java.util.Scanner class with a regular expression.
Hi article is nice, can we parsing String to int in java 8 ?
i was searching for the information then i found very good information here, it best way to convert string to int i hope this may be use full.