Results 1 to 8 of 8
Thread: Method- error?
- 10-16-2011, 02:09 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
- 10-16-2011, 02:12 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Method- error?
move the return count to the end of your method and count your brackets ;D
- 10-16-2011, 02:14 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 02:25 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: Method- error?
Its outside of the for loop now, right? What am I doing wrong?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; }
- 10-16-2011, 02:36 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: Method- error?
for some reason on my teacher's site it still says compilation 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; } }
- 10-16-2011, 02:50 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
-
Re: Method- error?
- 10-16-2011, 03:01 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
what does this error method mean?
By berkeley in forum New To JavaReplies: 1Last Post: 06-03-2010, 11:19 AM -
toString() method error?
By blueduiker in forum New To JavaReplies: 6Last Post: 02-07-2010, 03:19 AM -
Getting error when using recrusive method
By jurka in forum New To JavaReplies: 10Last Post: 02-13-2009, 03:15 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Why am I getting an error when calling this method?
By CirKuT in forum New To JavaReplies: 10Last Post: 09-18-2008, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks