0

I have an activity where the values are retrieved from the ResultSet and I store them in an array as:

flat[i] = rs.getDouble(3);
flng[i] = rs.getDouble(4);
i++;
Intent i = new Intent(MapID.this, map.class);
Bundle bundle=new Bundle();
bundle.putDoubleArray("Lat", flat);
bundle.putDoubleArray("Long", flng);
i.putExtras(bundle);
startActivity(i);

On the other class I get the values by:

Bundle bundle = getIntent().getExtras();
flat = bundle.getDoubleArray("Lat");

I want to convert the values to String something like:

String xx = new Double(flat).toString();
Blo
  • 11,903
  • 5
  • 45
  • 99
Sukan
  • 346
  • 1
  • 3
  • 19

3 Answers3

1

In Java, the + operator is defined for primitives. You can simply do something like String xx = "" + myDouble;

Although it is recommended to use the String methods like this : String xx = String.valueOf(myDouble);

Gladhus
  • 910
  • 1
  • 13
  • 24
1

I don't know what did you want that convert the values to String. If you want to convert single double number to String, you can use this:

String.valueOf(double);

But I think you need to changed all numbers of the double array to String, you may use this:

Arrays.toString(double[]);

If you need to put the all result's data to bundle, you may use this:

Bundle.putSparseParcelableArray(String key, SparseArray<? extends Parcelable> value);

SparseArray is very useful in android. It likes java.util.Map, but the key must be integer.

Aaron Lee
  • 751
  • 2
  • 8
  • 18
0

Use String.valueOf on each element of your array and store it to another String array like this:

double[] flat = bundle.getDoubleArray("Lat");
String[] flatAsStrings = new String[flat.length];

for (int i = 0; i < flat.length; i++) {
    flatAsStrings[i] = String.valueOf(flat[i]);
}

To simply print these float values without converting it to Strings, you can do it like this:

double[] flat = bundle.getDoubleArray("Lat");
System.out.println(Arrays.toString(flat));

Will print something like (depends on your input):

[1.0, 2.0, 3.0, 4.0, 5.0]
bobbel
  • 3,327
  • 2
  • 26
  • 43
  • yes Can you please explain that i want the values from the array as one after one and to store it in separate strings – Sukan Apr 01 '14 at 11:08
  • I have getting a nullpointer Exception in resultset any idea? `while(rs.next()) { flat[i]=rs.getDouble(3); flng[i]=rs.getDouble(4); i++; }` Query Looks correct unless I tried as Array – Sukan Apr 01 '14 at 11:53
  • What do you mean with **in** ResultSet? Can't you access `rs.next()`? Or are you not able to store the result of `rs.getDouble(x)` to the variable? How do you define your Arrays `flat` and `flng`? – bobbel Apr 01 '14 at 12:04
  • Yes.>I cant able to store the `resultset` values I declare it as `double[] flat;` `double[] flng;` – Sukan Apr 01 '14 at 12:05
  • So you didn't initialize them! You have to write something like `double[] flat = new double[42];` to initialize the array with 42 elements. I don't know, if you know, how many elements you will get. A `List` could be a better option. – bobbel Apr 01 '14 at 12:12