How to convert string to int in java
How to convert string to int in java
how to convert String into Integer in java
I want to convert String to int If the String contain character it will return 0 Else it will return the number For example the conversion of (ad12) is 0 But (12) Is 12
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
All you need to do is use Integer class to get int value of valid integer containing string.
You can attempt to parse it and catch the exception
You can also use IntegerStringConverter class instance to convert Integer to String or vice versa. IntegerStringConverter isc=new IntegerStringConverter(); int a=isc.fromString(«Your String»); //It converts String to Integer String str=isc.toString(Integer Value); //It converts Integer to String
Not the answer you’re looking for? Browse other questions tagged java or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How do you convert from a string to an int in Java?
So I have to make a card project that takes a string such as «Six of Hearts» and converts that into an integer array based on the value of the number (six) and the suit (hearts). I’m hitting a wall as to how to get java to take the string «six» and output 6. Any hints?
Edit: Clubs = 0; Spades = 3; Hearts = 2; Diamonds = 1;
7 Answers 7
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You could use a Map. You could also use a case or if else statements to accomplish this.
Case sensitivity will matter here so make sure the key matches whatever you pass to access the value.
This is typically something you would use an enum for:
Code for card values omitted. Follows the same approach.
There are multiple ways you can do this, but in my opinion the simplest way would be to have a map of where the string key is the word for the number, i.e. «six», and the value returned is the integer value.
So your code would look like this
then whenever you need the digit, you do numMap.get(«two»)
You can mix it up a little bit.
The same goes for converting Shape into actual number that represent the shape.
Most efficient way of converting String to Integer in java
There are many ways of converting a String to an Integer object. Which is the most efficient among the below:
My usecase needs to create wrapper Integer objects. meaning no primitive int. and the converted data is used for read-only.
11 Answers 11
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If efficiency is your concern, then creating an Integer object is much more expensive than parsing it. If you have to create an Integer object, I wouldn’t worry too much about how it is parsed.
Note 2: If you are reading raw data e.g. bytes from a file or network, the conversion of these bytes to a String is relatively expensive as well. If you are going to write a custom parser I suggest bypassing the step of conversing to a string and parse the raw source data.
Note 3: If you are creating an Integer so you can put it in a collection, you can avoid this by using GNU Trove (trove4j) which allows you to store primitives in collections, allowing you to drop the Integer creation as well.
Ideally, for best performance you want to avoid creating any objects at all.
Best way to convert String to Integer or int
This might asked too many times before but.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The java.lang.NumberFormatException is due to the fact that you are trying to convert a double literal to a an int type.
There is not a best way but the usage of
depends on your need of a double primitive type or a Double object type.
If you need an integer type, you can round the double value like this:
or simply cast the double to obtain the integral part only
The new BigDecimal(str).intValue(); is surely the worst choiche because can lead to unespected results as stated in the Oracle documentation (see the bold):
Converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting «BigInteger» is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this BigDecimal value as well as return a result with the opposite sign.
Unrelated to the question, there is a sometime faster way when you need to instantiate an Integer from an int value. From the java.lang.Integer class documentation:
public static Integer valueOf(int i)
Convert hex string to int
I am trying to convert a string that is 8 characters long of hex code into an integer so that I can do int comparison instead of string comparisons over a lot of different values.
I know this is fairly trivial in C++ but I need to do it in Java. The test case I need to satisfy is essentially to Convert «AA0F245C» to int and then back to that string so that I know it is converting right.
I have tried the following:
I have also tried doing it two characters at a time and multiplying the results, which the conversion works but the number is not right.
I can not use third party libraries just so you know, so this has to be done in Java standard libraries.
Thank you for your help in advance.
9 Answers 9
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It’s simply too big for an int (which is 4 bytes and signed).
you may use like that
The maximum value that a Java Integer can handle is 2147483657, or 2^31-1. The hexadecimal number AA0F245C is 2853119068 as a decimal number, and is far too large, so you need to use
to make it work.
you can easily do it with parseInt with format parameter.
This is the right answer:
myPassedColor = «#ffff8c85» int colorInt = Color.parseColor(myPassedColor)
Источники информации:
- http://stackoverflow.com/questions/27237325/how-do-you-convert-from-a-string-to-an-int-in-java?rq=1
- http://stackoverflow.com/questions/1030479/most-efficient-way-of-converting-string-to-integer-in-java
- http://stackoverflow.com/questions/31879832/best-way-to-convert-string-to-integer-or-int
- http://stackoverflow.com/questions/11194513/convert-hex-string-to-int