# Thread: Selecting the greatest "double" in a string array

1. Member
Join Date
Oct 2009
Posts
5
Rep Power
0

## 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:

Java 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.

:)

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Read the API specs for the Double.parseDouble method.

3. That's not a String array.

4. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
a is a string array,

thank you for the replies, can you please give me the code pls?
does this code look right?
Java 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

5. Originally Posted by gangsterooseven
a is a String array.
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:
Java 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.

6. 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.
Java 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);
}```

7. Member
Join Date
Oct 2009
Posts
5
Rep Power
0
Thank you very much,
it makes so much sense and is very simplified,

cheers:)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•