Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default 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. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the API specs for the Double.parseDouble method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That's not a String array.

  4. #4
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gangsterooseven View Post
    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #7
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Thumbs up

    Thank you very much,
    it makes so much sense and is very simplified,

    cheers:)

Similar Threads

  1. Replies: 3
    Last Post: 05-21-2009, 03:48 PM
  2. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. represent Double as "" instead of 0.0 in .jsp page without javascript
    By Tokajac in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2008, 02:49 PM

Posting Permissions

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