How can I convert a String
value to an int
type?
"1234" → 1234
-
11Mod note: This question has 30 answers and another 82 deleted answers, most of which were removed for repeating existing answers. If you are considering adding a new answer to this question, please ensure that you've read all the existing answers and confirmed that your answer adds something new and useful.Ryan M– Ryan M ♦03/02/2023 01:50:04Commented Mar 2, 2023 at 1:50
-
7And seriously ... just 'cos you can think of yet another wacky way to do the conversion ... don't imagine that telling everyone about it is actually a helpful / useful thing to do.Stephen C– Stephen C03/02/2023 06:08:17Commented Mar 2, 2023 at 6:08
31 Answers 31
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)
-
46In addition to catching a NumberFormatException, the user should also be careful about the length of the strings they're passing in; if they're long enough to overflow an integer, they might want to consider using Long::parseLong instead.Allison– Allison01/17/2018 09:37:40Commented Jan 17, 2018 at 9:37
-
This is the most complete explanation and the comments by Allison covers the problem of Long numbers passed as a String!!!!tamegajr– tamegajr03/29/2023 12:22:28Commented Mar 29, 2023 at 12:22
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:
valueOf
returns a new or cached instance ofjava.lang.Integer
parseInt
returns primitiveint
.
The same is for all cases: Short.valueOf
/parseShort
, Long.valueOf
/parseLong
, etc.
-
89For the differences between the two methods, see this questionhertzsprung– hertzsprung05/19/2013 08:38:20Commented May 19, 2013 at 8:38
-
23
valueOf
method is justreturn valueOf(parseInt(string));
Paul Verest– Paul Verest10/28/2014 08:55:44Commented Oct 28, 2014 at 8:55
Well, a very important point to consider is that the Integer parser throws NumberFormatException as stated in Javadoc.
int foo;
String StringThatCouldBeANumberOrNot = "26263Hello"; //will throw exception
String StringThatCouldBeANumberOrNot2 = "26263"; //will not throw exception
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot);
} catch (NumberFormatException e) {
//Will Throw exception!
//do something! anything to handle the exception.
}
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot2);
} catch (NumberFormatException e) {
//No problem this time, but still it is good practice to care about exceptions.
//Never trust user input :)
//Do something! Anything to handle the exception.
}
It is important to handle this exception when trying to get integer values from split arguments or dynamically parsing something.
Do it manually:
public static int strToInt(String str) {
int i = 0;
int num = 0;
boolean isNeg = false;
// Check for negative sign; if it's there, set the isNeg flag
if (str.charAt(0) == '-') {
isNeg = true;
i = 1;
}
// Process each character of the string;
while (i < str.length()) {
num *= 10;
num += str.charAt(i++) - '0'; // Minus the ASCII code of '0' to get the value of the charAt(i++).
}
if (isNeg)
num = -num;
return num;
}
-
28What if the input is greater than 2^32? What if the input contains non-numeric characters?yohm– yohm10/22/2014 03:43:16Commented Oct 22, 2014 at 3:43
-
100One of the things a programmer must learn on joining the workforce, if not before, is never to re-invent wheels. This may be a fun exercise, but don't expect your code to pass code review if you do this kind of thing in a commercial setting.Dawood ibn Kareem– Dawood ibn Kareem01/01/2016 04:16:17Commented Jan 1, 2016 at 4:16
-
@yohm those are special case; you can handle with long and some regex; however, by then you can use parseInt.Billz– Billz01/01/2016 05:18:29Commented Jan 1, 2016 at 5:18
-
53-1 Sorry, but this is a pretty poor algorithm, with lots of limitations, no error handling, and some weird anomalies (eg "" gives an exception, "-" will produce 0, and "+" produces -5). Why would anyone choose this over
Integer.parseInt(s)
? - I see the point about this being an interview question, but a) that doesn't imply you'd do it this way (which is what the questioner asked), and b) this answer's a pretty bad example anyway.SusanW– SusanW07/28/2016 17:27:03Commented Jul 28, 2016 at 17:27 -
2-1 because what if I want to parse a base 31 int? Integer.parseInt(str, 31) is a one liner to do that. Slightly facetious comment, but serious point underneath. Never re-invent wheels when someone else has already put the work inNathan Adams– Nathan Adams05/01/2019 18:17:06Commented May 1, 2019 at 18:17
An alternate solution is to use Apache Commons' NumberUtils:
int num = NumberUtils.toInt("1234");
The Apache utility is nice because if the string is an invalid number format then 0 is always returned. Hence saving you the try catch block.
-
43You rarely want 0 to be used when an invalid number is parsed.wnoise– wnoise03/22/2016 15:18:18Commented Mar 22, 2016 at 15:18
-
15@Ryboflavin No, it doesn't. One of those is a well-defined language semantic, and the other is an exceptionetherous– etherous06/01/2017 22:25:45Commented Jun 1, 2017 at 22:25
-
2You can also specify your own default value with the overloaded method NumberUtils.toInt(String, int);Yann Vo– Yann Vo04/20/2022 09:31:35Commented Apr 20, 2022 at 9:31
-
You would not want to return a defined and valid number for an undefined state, You still can set the result to 0 in the try/catch block, if this is appropriate for your code. If 0 is a valid result, it is senseless using it as an identifier for invalid results. If 0 is not in your range of valid numbers, you will still have to check for a 0 result outside.syck– syck12/05/2024 17:59:38Commented Dec 5, 2024 at 17:59
Integer.decode
You can also use public static Integer decode(String nm) throws NumberFormatException
.
It also works for bases 8 and 16:
// base 10
Integer.parseInt("12"); // 12 - int
Integer.valueOf("12"); // 12 - Integer
Integer.decode("12"); // 12 - Integer
// base 8
// 10 (0,1,...,7,10,11,12)
Integer.parseInt("12", 8); // 10 - int
Integer.valueOf("12", 8); // 10 - Integer
Integer.decode("012"); // 10 - Integer
// base 16
// 18 (0,1,...,F,10,11,12)
Integer.parseInt("12", 16); // 18 - int
Integer.valueOf("12", 16); // 18 - Integer
Integer.decode("#12"); // 18 - Integer
Integer.decode("0x12"); // 18 - Integer
Integer.decode("0X12"); // 18 - Integer
// base 2
Integer.parseInt("11", 2); // 3 - int
Integer.valueOf("11", 2); // 3 - Integer
If you want to get int
instead of Integer
you can use:
Unboxing:
int val = Integer.decode("12");
intValue()
:Integer.decode("12").intValue();
-
It also decodes negative numbers.Mama africa– Mama africa05/18/2025 12:52:08Commented May 18 at 12:52
Currently I'm doing an assignment for college, where I can't use certain expressions, such as the ones above, and by looking at the ASCII table, I managed to do it. It's a far more complex code, but it could help others that are restricted like I was.
The first thing to do is to receive the input, in this case, a string of digits; I'll call it String number
, and in this case, I'll exemplify it using the number 12, therefore String number = "12";
Another limitation was the fact that I couldn't use repetitive cycles, therefore, a for
cycle (which would have been perfect) can't be used either. This limits us a bit, but then again, that's the goal. Since I only needed two digits (taking the last two digits), a simple charAt
solved it:
// Obtaining the integer values of the char 1 and 2 in ASCII
int semilastdigitASCII = number.charAt(number.length() - 2);
int lastdigitASCII = number.charAt(number.length() - 1);
Having the codes, we just need to look up at the table, and make the necessary adjustments:
double semilastdigit = semilastdigitASCII - 48; // A quick look, and -48 is the key
double lastdigit = lastdigitASCII - 48;
Now, why double? Well, because of a really "weird" step. Currently we have two doubles, 1 and 2, but we need to turn it into 12, there isn't any mathematic operation that we can do.
We're dividing the latter (lastdigit) by 10 in the fashion 2/10 = 0.2
(hence why double) like this:
lastdigit = lastdigit / 10;
This is merely playing with numbers. We were turning the last digit into a decimal. But now, look at what happens:
double jointdigits = semilastdigit + lastdigit; // 1.0 + 0.2 = 1.2
Without getting too into the math, we're simply isolating units the digits of a number. You see, since we only consider 0-9, dividing by a multiple of 10 is like creating a "box" where you store it (think back at when your first grade teacher explained you what a unit and a hundred were). So:
int finalnumber = (int) (jointdigits*10); // Be sure to use parentheses "()"
And there you go. You turned a String of digits (in this case, two digits), into an integer composed of those two digits, considering the following limitations:
- No repetitive cycles
- No "Magic" Expressions such as parseInt
-
14It’s not clear what kind of problem this answer tries to solve, first, why anyone should ever have that restriction you describe, second, why you have to look at an ASCII table as you can simply use
'0'
for the character instead of48
and never have to bother with its actual numeric value. Third, the entire detour withdouble
values makes no sense at all as you are dividing by ten, just to multiply with ten afterwards. The result simply issemilastdigit * 10 + lastdigit
as learnt in elementary school, when the decimal system was introduced...Holger– Holger03/04/2016 10:47:41Commented Mar 4, 2016 at 10:47
Methods to do that:
Integer.parseInt(s)
Integer.parseInt(s, radix)
Integer.parseInt(s, beginIndex, endIndex, radix)
Integer.parseUnsignedInt(s)
Integer.parseUnsignedInt(s, radix)
Integer.parseUnsignedInt(s, beginIndex, endIndex, radix)
Integer.valueOf(s)
Integer.valueOf(s, radix)
Integer.decode(s)
NumberUtils.toInt(s)
NumberUtils.toInt(s, defaultValue)
Integer.valueOf
produces an Integer
object and all other methods a primitive int.
The last two methods are from commons-lang3 and a big article about converting here.
Whenever there is the slightest possibility that the given String does not contain an Integer, you have to handle this special case. Sadly, the standard Java methods Integer::parseInt
and Integer::valueOf
throw a NumberFormatException
to signal this special case. Thus, you have to use exceptions for flow control, which is generally considered bad coding style.
In my opinion, this special case should be handled by returning an empty Optional<Integer>
. Since Java does not offer such a method, I use the following wrapper:
private Optional<Integer> tryParseInteger(String string) {
try {
return Optional.of(Integer.valueOf(string));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
Example usage:
// prints "12"
System.out.println(tryParseInteger("12").map(i -> i.toString()).orElse("invalid"));
// prints "-1"
System.out.println(tryParseInteger("-1").map(i -> i.toString()).orElse("invalid"));
// prints "invalid"
System.out.println(tryParseInteger("ab").map(i -> i.toString()).orElse("invalid"));
While this is still using exceptions for flow control internally, the usage code becomes very clean. Also, you can clearly distinguish the case where -1
is parsed as a valid value and the case where an invalid String could not be parsed.
Use Integer.parseInt(yourString)
.
Remember the following things:
Integer.parseInt("1");
// ok
Integer.parseInt("-1");
// ok
Integer.parseInt("+1");
// ok
Integer.parseInt(" 1");
// Exception (blank space)
Integer.parseInt("2147483648");
// Exception (Integer is limited to a maximum value of 2,147,483,647)
Integer.parseInt("1.1");
// Exception (. or , or whatever is not allowed)
Integer.parseInt("");
// Exception (not 0 or something)
There is only one type of exception: NumberFormatException
We can use the parseInt(String str)
method of the Integer
wrapper class for converting a String value to an integer value.
For example:
String strValue = "12345";
Integer intValue = Integer.parseInt(strVal);
The Integer
class also provides the valueOf(String str)
method:
String strValue = "12345";
Integer intValue = Integer.valueOf(strValue);
We can also use toInt(String strValue)
of NumberUtils Utility Class for the conversion:
String strValue = "12345";
Integer intValue = NumberUtils.toInt(strValue);
I have a solution, but I do not know how effective it is. But it works well, and I think you could improve it. On the other hand, I did a couple of tests with JUnit which step correctly. I attached the function and testing:
public static Integer str2Int(String str) {
Integer result = null;
if (null == str || 0 == str.length()) {
return null;
}
try {
result = Integer.parseInt(str);
}
catch (NumberFormatException e) {
String negativeMode = "";
if (str.indexOf('-') != -1)
negativeMode = "-";
str = str.replaceAll("-", "");
if (str.indexOf('.') != -1) {
str = str.substring(0, str.indexOf('.'));
if (str.length() == 0) {
return (Integer) 0;
}
}
String strNum = str.replaceAll("[^\\d]", "" );
if (0 == strNum.length()) {
return null;
}
result = Integer.parseInt(negativeMode + strNum);
}
return result;
}
Testing with JUnit:
@Test
public void testStr2Int() {
assertEquals("is numeric", (Integer) (-5), Helper.str2Int("-5"));
assertEquals("is numeric", (Integer) 50, Helper.str2Int("50.00"));
assertEquals("is numeric", (Integer) 20, Helper.str2Int("$ 20.90"));
assertEquals("is numeric", (Integer) 5, Helper.str2Int(" 5.321"));
assertEquals("is numeric", (Integer) 1000, Helper.str2Int("1,000.50"));
assertEquals("is numeric", (Integer) 0, Helper.str2Int("0.50"));
assertEquals("is numeric", (Integer) 0, Helper.str2Int(".50"));
assertEquals("is numeric", (Integer) 0, Helper.str2Int("-.10"));
assertEquals("is numeric", (Integer) Integer.MAX_VALUE, Helper.str2Int("" + Integer.MAX_VALUE));
assertEquals("is numeric", (Integer) Integer.MIN_VALUE, Helper.str2Int("" + Integer.MIN_VALUE));
assertEquals("Not is numeric", null, Helper.str2Int("czv.,xcvsa"));
/**
* Dynamic test
*/
for (Integer num = 0; num < 1000; num++) {
for (int spaces = 1; spaces < 6; spaces++) {
String numStr = String.format("%0" + spaces + "d", num);
Integer numNeg = num * -1;
assertEquals(numStr + ": is numeric", num, Helper.str2Int(numStr));
assertEquals(numNeg + ": is numeric", numNeg, Helper.str2Int("- " + numStr));
}
}
}
You can also begin by removing all non-numerical characters and then parsing the integer:
String mystr = mystr.replaceAll("[^\\d]", "");
int number = Integer.parseInt(mystr);
But be warned that this only works for non-negative numbers.
-
21This will cause
-42
to be parsed as42
.user289086– user28908610/11/2014 14:00:14Commented Oct 11, 2014 at 14:00 -
This will also misparse non-integral numbers (e.g.,
4.2
as42
) if that could be in your data.03/02/2023 01:42:16Commented Mar 2, 2023 at 1:42
Google Guava has Ints.tryParse(String), which returns null
if the string couldn't be parsed, for example:
Integer fooInt = Ints.tryParse(fooString);
if (fooInt != null) {
...
}
Apart from the previous answers, I would like to add several functions. These are results while you use them:
public static void main(String[] args) {
System.out.println(parseIntOrDefault("123", 0)); // 123
System.out.println(parseIntOrDefault("aaa", 0)); // 0
System.out.println(parseIntOrDefault("aaa456", 3, 0)); // 456
System.out.println(parseIntOrDefault("aaa789bbb", 3, 6, 0)); // 789
}
Implementation:
public static int parseIntOrDefault(String value, int defaultValue) {
int result = defaultValue;
try {
result = Integer.parseInt(value);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int endIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex, endIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
As mentioned, Apache Commons' NumberUtils
can do it. It returns 0
if it cannot convert a string to an int.
You can also define your own default value:
NumberUtils.toInt(String str, int defaultValue)
Example:
NumberUtils.toInt("3244", 1) = 3244
NumberUtils.toInt("", 1) = 1
NumberUtils.toInt(null, 5) = 5
NumberUtils.toInt("Hi", 6) = 6
NumberUtils.toInt(" 32 ", 1) = 1 // Space in numbers are not allowed
NumberUtils.toInt(StringUtils.trimToEmpty(" 32 ", 1)) = 32;
You can use new Scanner("1244").nextInt()
. Or ask if even an int exists: new Scanner("1244").hasNextInt()
In programming competitions, where you're assured that the number will always be a valid integer, then you can write your own method to parse the input. This will skip all validation-related code (since you don't need any of that) and will be a bit more efficient.
- For valid positive integer:
private static int parseInt(String str) {
int i, n = 0;
for (i = 0; i < str.length(); i++) {
n *= 10;
n += str.charAt(i) - 48;
}
return n;
}
- For both positive and negative integers:
private static int parseInt(String str) {
int i = 0, n = 0, sign = 1;
if (str.charAt(0) == '-') {
i = 1;
sign = -1;
}
for (; i < str.length(); i++) {
n *= 10;
n += str.charAt(i) - 48;
}
return sign * n;
}
- If you are expecting whitespace before or after these numbers,
then make sure to do a
str = str.trim()
before processing further.
For a normal String
, you can use:
int number = Integer.parseInt("1234");
For a StringBuilder
and StringBuffer
, you can use:
Integer.parseInt(myBuilderOrBuffer.toString());
I am a little bit surprised that nobody mentioned the Integer constructor that takes String as a parameter.
So, here it is:
String myString = "1234";
int i1 = new Integer(myString);
Of course, the constructor will return type Integer
, and an unboxing operation converts the value to int
.
Note 1: It's important to mention: This constructor calls the parseInt
method.
public Integer(String var1) throws NumberFormatException {
this.value = parseInt(var1, 10);
}
Note 2: It's deprecated: @Deprecated(since="9")
- JavaDoc.
-
6Integer constructors are deprecatedBasilevs– Basilevs08/27/2020 17:19:46Commented Aug 27, 2020 at 17:19
The two main ways to do this are using the method valueOf()
and method parseInt()
of the Integer
class.
Suppose you are given a String like this
String numberInString = "999";
Then you can convert it into integer by using
int numberInInteger = Integer.parseInt(numberInString);
And alternatively, you can use
int numberInInteger = Integer.valueOf(numberInString);
But the thing here is, the method Integer.valueOf()
has the following implementation in Integer
class:
public static Integer valueOf(String var0, int var1) throws NumberFormatException {
return parseInt(var0, var1);
}
As you can see, the Integer.valueOf()
internally calls Integer.parseInt()
itself.
Also, parseInt()
returns an int
, and valueOf()
returns an Integer
-
2How does this add anything? There are several older answers that have already provided these approaches. Please read existing answers before posting.skomisa– skomisa04/12/2020 03:34:24Commented Apr 12, 2020 at 3:34
-
He is showing the efficiency of one is greater than the efficiency of the other.user1644002– user164400210/08/2021 17:05:55Commented Oct 8, 2021 at 17:05
-
1@user1644002 - Well if he (and you) think that, then he is (probably) incorrect. The JIT compiler should inline the
valueOf
->parseInt
call making the two versions equally efficient.Stephen C– Stephen C01/02/2022 01:55:24Commented Jan 2, 2022 at 1:55
There are different ways of converting a string int value into an Integer data type value. You need to handle NumberFormatException for the string value issue.
Integer.parseInt
foo = Integer.parseInt(myString);
Integer.valueOf
foo = Integer.valueOf(myString);
Using Java 8 Optional API
foo = Optional.ofNullable(myString).map(Integer::parseInt).get();
-
2Re "You need to handle NumberFormatException for the string value issue": How? By avoiding it happening altogether by one of these three methods? Can you make it more clear (without "Update: ", "Edit: " or similar)?Peter Mortensen– Peter Mortensen08/21/2020 16:06:19Commented Aug 21, 2020 at 16:06
-
1This is only two ways. Introduction of an optional doesn't change that you're using parseIntOneCricketeer– OneCricketeer02/28/2022 13:25:13Commented Feb 28, 2022 at 13:25
-
also using
Optional
just to hide anull
check is not best practiceuser85421– user8542103/14/2025 16:47:35Commented Mar 14 at 16:47
public class StringToInteger {
public static void main(String[] args) {
assert parseInt("123") == Integer.parseInt("123");
assert parseInt("-123") == Integer.parseInt("-123");
assert parseInt("0123") == Integer.parseInt("0123");
assert parseInt("+123") == Integer.parseInt("+123");
}
/**
* Parse a string to integer
*
* @param s the string
* @return the integer value represented by the argument in decimal.
* @throws NumberFormatException if the {@code string} does not contain a parsable integer.
*/
public static int parseInt(String s) {
if (s == null) {
throw new NumberFormatException("null");
}
boolean isNegative = s.charAt(0) == '-';
boolean isPositive = s.charAt(0) == '+';
int number = 0;
for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
if (!Character.isDigit(s.charAt(i))) {
throw new NumberFormatException("s=" + s);
}
number = number * 10 + s.charAt(i) - '0';
}
return isNegative ? -number : number;
}
}
You can have your own implementations for this, like:
public class NumericStringToInt {
public static void main(String[] args) {
String str = "123459";
int num = stringToNumber(str);
System.out.println("Number of " + str + " is: " + num);
}
private static int stringToNumber(String str) {
int num = 0;
int i = 0;
while (i < str.length()) {
char ch = str.charAt(i);
if (ch < 48 || ch > 57)
throw new NumberFormatException("" + ch);
num = num * 10 + Character.getNumericValue(ch);
i++;
}
return num;
}
}
-
2What are the magic numbers 48 and 57? Can't (named) constants be used?Peter Mortensen– Peter Mortensen08/21/2020 16:11:01Commented Aug 21, 2020 at 16:11
-
1@PeterMortensen
'0'
and'9'
(char
literals) would be far clearer; no need to even use constants.08/06/2024 19:10:50Commented Aug 6, 2024 at 19:10
This function accepts any param types as input
- then try to convert it
toString()
- then extract integer via regex
- and safe convert string to int
public int toInt(Object o) {
// input param is an integer :|
if (o instanceof Integer)
return (int) o;
// input param is (null) so return zero
if (o == null)
return 0;
// input param is boolean, so false = 0 \ true = 1
if (o instanceof Boolean)
return Boolean.TRUE.equals(o) ? 1 : 0;
// convert object to string
String str = "0";
if (o instanceof String) {
str = (String) o;
} else {
try {
str = o.toString();
} catch (Exception e) {}
}
// return zero if the string is empty
if (str == "")
return 0;
// detect and export numbers from the string
try {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
if ( m.find() ) {
str = m.group(0);
} else { // string not contains any numbers
str = "0";
}
} catch (Exception e) {
str = "0";
}
// java stores integers in 32-bit, so can not store more than 10 digits
if (str.length() > 19) {
str = str.substring(0, 19);
}
// convert string to integer
int result = 0;
try {
result = Integer.parseInt(str);
} catch (Exception e) {}
return result;
}
You can change
catch (Exception e) {}
to
catch (Exception e) { e.printStackTrace(); }
to show more detailed data about errors in logcat
Can handle inputs such as :
false
""
"00004"
" 51"
"74.6ab.cd"
"foo 654 bar"
Warning
This function will change (string)"ab2cd3ef4"
to (int)234
-
1by the way: no need to test if it is already a
String
; just call itstoString
method // in the code comment we see "more than 10 digits" but the code isstr.length() > 19
andstr.substring(0, 19)
-- the comment is correct // catchingException
instead of more specific exception is not recommended!user85421– user8542103/14/2025 16:32:08Commented Mar 14 at 16:32
import java.util.*;
public class strToint {
public static void main(String[] args) {
String str = "123";
byte barr[] = str.getBytes();
System.out.println(Arrays.toString(barr));
int result = 0;
for (int i = 0; i < barr.length; i++) {
// System.out.print(barr[i] + " ");
int ii = barr[i];
char a = (char) ii;
int no = Character.getNumericValue(a);
result = result * 10 + no;
System.out.println(result);
}
System.out.println("result:" + result);
}
}
-
4Some explanation would be in order.Peter Mortensen– Peter Mortensen09/02/2019 22:35:04Commented Sep 2, 2019 at 22:35
-
2With
getBytes
you're using the platform's default charset and risking multi-byte encodings. There's just no reason to do this. Also, this doesn't handle negative numbers.03/02/2023 01:23:52Commented Mar 2, 2023 at 1:23
For Android developers ending up here these are various solutions for Kotlin:
// Throws exception if number has bad form
val result1 = "1234".toInt()
// Will be null if number has bad form
val result2 = "1234".toIntOrNull()
// Will be the given default if number has bad form
val result3 = "1234"
.toIntOrNull() ?: -1
// Will be return of the run block if number has bad form
val result4 = "1234"
.toIntOrNull()
?: run {
// some code
// return an Int
}
// Ignores any none-digit character in string
val result5 = "12abc34"
.filter { it.isDigit() }
.joinToString(separator="")
.toIntOrNull()
As already answered above by Rob Hruska.
You can convert a String to an int in Java using Integer.parseInt(). Here's a quick example:
String value = "1234";
int number = Integer.parseInt(value);
Make sure the string contains a valid integer, otherwise a NumberFormatException will be thrown. If you're unsure about the input, wrap it with a try-catch block to handle errors safely.
With Java 11, there are several ways to convert an int
to a String
type:
1) Integer.parseInt()
String str = "1234";
int result = Integer.parseInt(str);
2) Integer.valueOf()
String str = "1234";
int result = Integer.valueOf(str).intValue();
3) Integer constructor
String str = "1234";
Integer result = new Integer(str);
4) Integer.decode
String str = "1234";
int result = Integer.decode(str);
I wrote this fast method to parse a string input into int or long. It is faster than the current JDK 11 Integer.parseInt or Long.parseLong. Although, you only asked for int, I also included the long parser. The code parser below requires that the parser's method must be small for it to operate quickly. An alternative version is below the test code. The alternative version is pretty quick and it does not depend on the size of the class.
This class checks for overflow, and you could customize the code to adapt to your needs. An empty string will yield 0 with my method but that is intentional. You can change that to adapt your case or use as is.
This is only the part of the class where parseInt and parseLong are needed. Note that this only deals with base 10 numbers.
The test code for the int parser is below the code below.
/*
* Copyright 2019 Khang Hoang Nguyen
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* @author: Khang Hoang Nguyen - [email protected].
**/
final class faiNumber{
private static final long[] longpow = {0L, 1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L, 1000000000L,
10000000000L, 100000000000L, 1000000000000L, 10000000000000L, 100000000000000L,
1000000000000000L, 10000000000000000L, 100000000000000000L, 1000000000000000000L,
};
private static final int[] intpow = { 0, 1, 10, 100, 1000, 10000,
100000, 1000000, 10000000, 100000000, 1000000000
};
/**
* parseLong(String str) parse a String into Long.
* All errors throw by this method is NumberFormatException.
* Better errors can be made to tailor to each use case.
**/
public static long parseLong(final String str) {
final int length = str.length();
if (length == 0)
return 0L;
char c1 = str.charAt(0);
int start;
if (c1 == '-' || c1 == '+') {
if (length == 1)
throw new NumberFormatException(String.format("Not a valid long value. Input '%s'.", str));
start = 1;
} else {
start = 0;
}
/*
* Note: if length > 19, possible scenario is to run through the string
* to check whether the string contains only valid digits.
* If the check had only valid digits then a negative sign meant underflow, else, overflow.
*/
if (length - start > 19)
throw new NumberFormatException(String.format("Not a valid long value. Input '%s'.", str));
long c;
long out = 0L;
for ( ; start < length; start++) {
c = (str.charAt(start) ^ '0');
if (c > 9L)
throw new NumberFormatException( String.format("Not a valid long value. Input '%s'.", str) );
out += c * longpow[length - start];
}
if (c1 == '-') {
out = ~out + 1L;
// If out > 0 number underflow(supposed to be negative).
if (out > 0L)
throw new NumberFormatException(String.format("Not a valid long value. Input '%s'.", str));
return out;
}
// If out < 0 number overflow (supposed to be positive).
if (out < 0L)
throw new NumberFormatException(String.format("Not a valid long value. Input '%s'.", str));
return out;
}
/**
* parseInt(String str) parse a string into an int.
* return 0 if string is empty.
**/
public static int parseInt(final String str) {
final int length = str.length();
if (length == 0)
return 0;
char c1 = str.charAt(0);
int start;
if (c1 == '-' || c1 == '+') {
if (length == 1)
throw new NumberFormatException(String.format("Not a valid integer value. Input '%s'.", str));
start = 1;
} else {
start = 0;
}
int out = 0; int c;
int runlen = length - start;
if (runlen > 9) {
if (runlen > 10)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
c = (str.charAt(start) ^ '0'); // <- Any number from 0 - 255 ^ 48 will yield greater than 9 except 48 - 57
if (c > 9)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
if (c > 2)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
out += c * intpow[length - start++];
}
for ( ; start < length; start++) {
c = (str.charAt(start) ^ '0');
if (c > 9)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
out += c * intpow[length - start];
}
if (c1 == '-') {
out = ~out + 1;
if (out > 0)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
return out;
}
if (out < 0)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
return out;
}
}
Test Code Section. This should take around 200 seconds or so.
// Int Number Parser Test;
long start = System.currentTimeMillis();
System.out.println("INT PARSER TEST");
for (int i = Integer.MIN_VALUE; i != Integer.MAX_VALUE; i++){
if (faiNumber.parseInt(""+i) != i)
System.out.println("Wrong");
if (i == 0)
System.out.println("HalfWay Done");
}
if (faiNumber.parseInt("" + Integer.MAX_VALUE) != Integer.MAX_VALUE)
System.out.println("Wrong");
long end = System.currentTimeMillis();
long result = (end - start);
System.out.println(result);
// INT PARSER END */
An alternative method which is also very fast. Note that array of int pow is not used, but a mathematical optimization of multiply by 10 by bit shifting.
public static int parseInt(final String str) {
final int length = str.length();
if (length == 0)
return 0;
char c1 = str.charAt(0);
int start;
if (c1 == '-' || c1 == '+') {
if (length == 1)
throw new NumberFormatException(String.format("Not a valid integer value. Input '%s'.", str));
start = 1;
} else {
start = 0;
}
int out = 0;
int c;
while (start < length && str.charAt(start) == '0')
start++; // <-- This to disregard leading 0. It can be
// removed if you know exactly your source
// does not have leading zeroes.
int runlen = length - start;
if (runlen > 9) {
if (runlen > 10)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
c = (str.charAt(start++) ^ '0'); // <- Any number from 0 - 255 ^ 48 will yield greater than 9 except 48 - 57
if (c > 9)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
if (c > 2)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
out = (out << 1) + (out << 3) + c; // <- Alternatively this can just be out = c or c above can just be out;
}
for ( ; start < length; start++) {
c = (str.charAt(start) ^ '0');
if (c > 9)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
out = (out << 1) + (out << 3) + c;
}
if (c1 == '-') {
out = ~out + 1;
if (out > 0)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
return out;
}
if (out < 0)
throw new NumberFormatException(String.format("Not a valid integer value. Input: '%s'.", str));
return out;
}
-
Also, to clarify. The reason why having the power array method ran faster is that Java cached the result for this type of test code. I tested, in a real-life situation, using bit shift will work way faster.Kevin Ng– Kevin Ng09/07/2019 06:14:10Commented Sep 7, 2019 at 6:14
-
Why do you have
if (c > 9)
followed immediately byif (c > 2)
with the exact same contents?03/02/2023 01:22:48Commented Mar 2, 2023 at 1:22
Explore related questions
See similar questions with these tags.