Results 1 to 8 of 8

Thread: Method- error?

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Method- error?

    Create a method called countZeros, which takes an integer array as an input parameter and returns the number of zero entries in the array.

    Example:
    data:{0, 6, 7, 0, 5, 9, 0} ==> 3

    Java Code:
    public int countZeros (int [ ] data) 
    {
      int count=0;
      for(int j=0;j<data.length;j++){
        if(data[j]==0){
          ++count;}
        return count;
      }
    }
    }

    It keeps saying the system has detected compilation errors, but I'm just not seeing them.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Method- error?

    move the return count to the end of your method and count your brackets ;D

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,768
    Blog Entries
    7
    Rep Power
    21

    Default Re: Method- error?

    Your "return count;" statement is located in the for-loop; move it to the location where the loop has finished. Also, remove that last right curly bracket (and put your brackets in a more prominent place).

    kind regards.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Method- error?

    Java Code:
    public int countZeros (int [ ] data)
    {
      int count=0;
      for(int j=0;j<data.length;j++)
    {
        if(data[j]==0)
          ++count;
    }
    return count;
    }
    Its outside of the for loop now, right? What am I doing wrong?

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Method- error?

    Java Code:
    public int countZeros (int [ ] data) 
    {
      int count=0;
      for(int j=0;j<data.length;j++)
      {
        if(data[j]==0)
        {
          ++count;
        }
      }
      return count;
    }
      }
    for some reason on my teacher's site it still says compilation error...

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,768
    Blog Entries
    7
    Rep Power
    21

    Default Re: Method- error?

    Quote Originally Posted by katiebear128 View Post
    for some reason on my teacher's site it still says compilation error...
    From the code you have shown us there is one right curly bracket too many (count them).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Method- error?

    Quote Originally Posted by katiebear128 View Post
    for some reason on my teacher's site it still says compilation error...
    Can you tell us the whole error message? Also, what happens when you compile it on your own box?

  8. #8
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Method- error?

    Thanks for all the help! I got rid of the last curly bracket, then logged out and back into my teacher's site, which fixed the problem...the site usually has issues, unfortunately..

Similar Threads

  1. what does this error method mean?
    By berkeley in forum New To Java
    Replies: 1
    Last Post: 06-03-2010, 12:19 PM
  2. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 04:19 AM
  3. Getting error when using recrusive method
    By jurka in forum New To Java
    Replies: 10
    Last Post: 02-13-2009, 04:15 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. Replies: 10
    Last Post: 09-18-2008, 10:41 AM

Tags for this Thread

Posting Permissions

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