why is minimum value from stringArray using colections.min() not working? - android

I've been using this method to find the minimum value of doubleArray :
colections.min()
but when i'm using this method to string Array, it got the wrong result. Likes this :
My stringArray value : {2.3 Km, 10,0 Km, 4.0 Km, 24,7 Km}
but when i call the result, its show 10.0 Km as minimum value instead 2,3 Km.
Can anybody tell me what is the perfect method that i can use to get the minimum value of the srtringArray ?
I know this is a very basic question, but i really need more explanation.
Thank you

A string beginning with 1 comes before a string beginning with 2. Just as it would with A and B. The minimum value of a string array is the string which comes first alphabetically. What are you expecting?

Related

Getting improper data while perform devide operation in android

I am doing as below :
var value = 0.0
value = ((3300/1000).toDouble())
But getting value as 3.0 instead of 3.3
Why ?
Where am I doing mistake ? What I have to do to get the value as 3.3 ?
you are dividing integers and then converting them to a double afterwards, which doesn't work like you want it to.
3300/1000
these are integers, so no decimal values. 3300/1000 is just 3
3300.0/1000.0
these are doubles, so they have decimal values

java Double.valueOf() returns number with one decimal place

I can't seem to get my head around this. If have tried the following approaches:
DecimalFormat df = new DecimalFormat("00000.00");
DecimalFormat df = new DecimalFormat("######.##");
But the following line always generates and IllegalArgumentException.
double price = Double.valueOf(df.format(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString()));
// Sample input passed is value of 200 or some other whole number.
item.setPrice(price);
It doesn't make sense as I only copied the obvious solutions in this forum. Most of you got the format() to work.
Originally, I didn't have these lines of code. I just call my setPrice() method after getting the item price. This works. However, Double.valueOf() has a nasty habit of using only one decimal position.
e.g. passed 200. I get 200.0 inside my item object. I figured by using DecimalFormat I could've prevented this but it appears this caused me MORE headaches instead.
When you say you pass 200 and you get 200.0, you mean you get it in a double value? If so, that doesn't matter - it's a number and 200 = 200.0 for double values.
format(...) turns a double value to a String value. You have it the other way round. That's why you get the Exception.
If the price variable is actually a double you should do
double price = Double.valueOf(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString())
But I think you want that the price is a String, then you should convert the text from the EditText to a double and that double back to a String with something like new DecimalFormat("0.00")

Floating Point is not correct

I am getting a value from server that is not containing any Floating point let say its
1234 and have to cvonvert it in Floating value with 2 decimal point like 12.34.
Right now what i am doing is getting value storing it in float that convert the current value 1234 to 1234.0
after that doing this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
RewardsBalance=df.format(tempB);
But with this i m having an issue that when i have value such that 1230 it results in 12.3 not that 12.30
but when i have value 1234 it gives the desired result that is 12.34
so what step i m missing any clue
12.3 and 12.30 are the same value. The problem is not the value but the code that incorrectly converts the right value to the wrong representation. You probably want "###.00". With "#", zero shows as absent.
Use this it will work
Two digits after point
Try this :
String.format("%.2f", your_value);
It will do just like you want
try this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("0.00");
df.format(tempB);
it will work fine.

Android Location.convert throwing illegal argument exception

This sentence is throwing IllegalArgumentException. I still can't figure out whats wrong with this format, its in [+-]DDD:MM.MMMMM as documentation says.
double latitude = Location.convert("-19:59.646");
Looks like the Android platform code for Location.convert(String coordinate) doesn't allow for minute decimal values greater than 59:
https://github.com/android/platform_frameworks_base/blob/master/location/java/android/location/Location.java#L255
Try using the FORMAT_SECONDS or FORMAT_DEGREES format instead as specified in the Location API docs:
http://developer.android.com/reference/android/location/Location.html
For example, your number in FORMAT_SECONDS (DDD:MM:SS.SSSSS) would be -19:59:38.76.
So the code would be:
double latitude = Location.convert("-19:59:38.76");
I verified that this works on Android using the FORMAT_SECONDS format.
You probably need to match that format exactly, so pad it with zeros.
double latitude = Location.convert( "-019:59.64600" );

Android SDK. String index out of bound exception

I FINALLY have the map and points(arrays) working for my app. Quick question: I have a fatal exception with substring(), a "stringIndexOutOfBoundException"
In general, what is that referring to?
An I going past the end of a string using substring()?
Thanks,
testing.substring(1,2);
(I want to parse each character to find specific characters)
I wouldn't use substring() for grabbing 1-length strings (which is just a single character), but rather charAt(int) for specific positions. If you need to go over all characters in the string, you're probably better off with by converting the whole thing to a char[] first (using toCharArray()) and iterate over that.
Yes, you're going past the end of your strings bounds generally.
The Java API even tells you so...
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
You should get used to using the API. It tells you what exceptions a method throws and why.
Try printing the Strings length and value before attempting substring. That'll help you see the problem.
For example...
String testing = "Hello StackOverflow";
System.out.println("Length of testing = " + testing.length);
System.out.println("Value of testing = " + testing);
testing.substring(1,2);
Like stated in the official doc here:
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Throws: IndexOutOfBoundsException - if beginIndex is negative or
larger than the length of this String object.

Categories

Resources