How can I convert a String
to an int
in Java?
My String contains only numbers and I want to return the number it represents.
For example, given the string "1234"
the result should be the number 1234
.
How can I convert a My String contains only numbers and I want to return the number it represents. For example, given the string |
||||
|
See the Java Documentation for more information. (If you have it in a |
|||||||||||||||||||||
|
For example, here are two ways:
There is a slight difference between these methods:
The same is for all cases: |
|||||||||
|
Well, a very important point to consider is that the Integer parser throws NumberFormatException as stated in Javadoc.
It is important to handle this exception when trying to get integer values from split arguments or dynamically parsing something. |
||||
|
Do it manually:
|
|||||||||||||||||||||
|
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 Another limitation was the fact that I couldn't use repetitive cicles, therefore, a
Having the codes, we just need to look up at the table, and make the necessary adjustments:
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.
What we're doing is divide the latter (lastdigit) by 10, in this fashion
This is merely playing with numbers. What we did here was turning the last digit into a decimal. But now, look at what happens:
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:
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:
|
|||||||||||||
|
An alternate solution is to use Apache Commons' NumberUtils:
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. |
|||||||||
|
Converting a string to an int is more complicated than just convertig a number. You have think about the following issues:
|
|||||
|
I'm 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:
Testing with JUnit:
|
||||
|
We can use the For example:
The
We can also use
|
||||
|
|
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 In my opinion, this special case should be handled by returning an
Usage:
While this is still using exceptions for flow control internally, the usage code becomes very clean. |
||||
|
You can also begin by removing all non numerical characters and then parsing the int:
But be warned that this only works for non negative numbers. |
|||||||||||||
|
You can use this code also, with some precautions.
Using a string constant for comparison or any sort of computing is always a good idea, because a constant never returns a null value. |
|||||||||
|
Guava has tryParse(String), which returns
|
|||
|
As mentioned Apache Commons You can also define your own default value.
example:
|
||||
|
Apart from these above answers, I would like to add several functions:
And here are results while you running them:
|
||||
|
For normal string you can use:
For String builder and String buffer you can use:
|
|||
|
Just for fun. You can use java 8
We just combine here
|
|||
|
make sure there is no non-numeric data in the string. For example, here are two ways:
|
||||
|
|
|||||
|
you need to use parseInt for
|
|||
|
|
||||
|
|
|||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?