# Selecting the greatest "double" in a string array

• 11-07-2009, 10:40 PM
gangsterooseven
Selecting the greatest "double" in a string array
Hi folks,

I am trying to achieve this:

i have a string array a, now say a={3.4,1.3,8.78,5.6,3.2}

i need to return the biggest double# in my output, i don't know how to convert this string to double and do a compare...

can somebody correct my approach:

Code:

```double first=a[0]; int next; for(int i=0;i<a.length;i++) {   next=i+1;   if(next<a.length)   {     if(first<a[i+1])     {     first=a[i+1];     }   } } System.out.println("biggest double ="+first);```
Now i don't know how to do type conversion and put the output correctly...i need to be outputting a string.

Thank u in advance for the help.

:)
• 11-07-2009, 10:43 PM
r035198x
Read the API specs for the Double.parseDouble method.
• 11-07-2009, 10:46 PM
Fubarable
That's not a String array.
• 11-07-2009, 11:37 PM
gangsterooseven
a is a string array,

thank you for the replies, can you please give me the code pls?
does this code look right?
Code:

```String output=""; double first=Double.parseDouble(a[0]); int next; for(int i=0;i<a.length;i++) {   next=i+1;   if(next<a.length)   {     if(first<Double.parseDouble(a[i+1]))     {     first=Double.parseDouble(a[i+1]);     }   } } output=Double.toString(first); System.out.println("biggest double ="+first);```
please correct me if im wrong
• 11-07-2009, 11:42 PM
Fubarable
Quote:

Originally Posted by gangsterooseven
a is a String array.

Quote:

I am trying to achieve this:
i have a string array a, now say a={3.4,1.3,8.78,5.6,3.2}
Possibly you posted the above incorrectly or in haste. If a were a String array, it would look like so:
Code:

`a = {"3.4", "1.3", "8.78", "5.6", "3.2"};`
Sorry for seeming picky, but in programming the details are important, especially when trying to debug another's program.

Best of luck.
• 11-07-2009, 11:50 PM
Fubarable
You're code works, but is a little cluttered (for my tastes). I'd simplify your code, get rid of next as it is unnecessary, change the variable name of "first" to "maxValue" because it only holds the first value for a short time and instead has been created to hold the max value. I would also do my double parse inside the loop once. Since you're so close, I'll post my simplified version.
Code:

```  public static void main(String[] args) {     String[] a = {"3.4", "1.3", "8.78", "5.6", "3.2"};     double maxValue = Double.parseDouble(a[0]);     for (int i = 0; i < a.length; i++) {       double current = Double.parseDouble(a[i]);       if (current > maxValue) {         maxValue = current;       }     }     System.out.println("biggest double = " + maxValue);   }```
• 11-08-2009, 12:37 AM
gangsterooseven
Thank you very much,
it makes so much sense and is very simplified,

cheers:)