Results 1 to 4 of 4
  1. #1
    javanubby is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default you cant do var.length - 1?

    Got an incompatible type error...
    Java Code:
    public static double mean (int x[ ]){
            int total = 0;
            for (int i=1; x.length - 1; i++)
                total = total + x[i];
            return (total/x.length-1);
        } //end method mean

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: you cant do var.length - 1?

    x.length-1 is an int expression; you need a boolean expression for the 'for-condition' part; you probable meant:

    Java Code:
    for (int i= 1; i < x.length-1; i++)
       // ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2012
    Location
    Banglore
    Posts
    4
    Rep Power
    0

    Default Re: you cant do var.length - 1?

    You have done mistake in for loop,
    at the place of condional statement(i.e. x.lingth - 1).
    To overcome this error,
    either,you have to give any boolean type value or any conditional statement there,
    then only the error can be removed.
    as like...

    for (int i=1; i< x.length ; i++)
    total = total + x[i];

  4. #4
    javanubby is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Difference between length() and length
    By mitra in forum New To Java
    Replies: 7
    Last Post: 07-28-2011, 01:46 AM
  2. help with length
    By ama91 in forum New To Java
    Replies: 15
    Last Post: 02-21-2011, 02:44 AM
  3. length and excepton
    By javagwcc in forum New To Java
    Replies: 5
    Last Post: 06-29-2010, 12:06 AM
  4. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 11:19 PM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:29 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
  •