Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with array home work.

 
Sora McKain
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment is to Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as
parameters and returns the count of how many elements from the array fall between the minimum and maximum
(inclusive). For example, in the array {14, 1, 22, 17, 36, 7, 43, 5}, for minimum value 4 and maximum value 17,
there are four elements whose values fall between 4 and 17.

I have a bit of code that looks like it should work, however i keep getting an error message that does not make sense to me, could someone help me understand why I am getting this message?


my code is:



the error message I get is

>javac InRange.java
InRange.java:26: incompatible types
found : int
required: int[]
return count;
^
1 error
>Exit code: 1

I know this should work so any help would be appreciated.

P.S I am pretty new to java so please dumb your answers down for me, Thank You!!


Sora McKain =D
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is telling exactly where the problem is (as it does in most cases).

You declared a method countInRange(...) to return an array of integers (int[]), though it actually returns count which is "just" an integer. Think about it - if you want a method to return number of elements that are in the given range, what type should the method return?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sora McKain wrote:
I know this should work so any help would be appreciated.


You'll save yourself a lot of frustration if you can learn to beat those thoughts down as soon as they start to pop up. ;)

If something doesn't work, it's because it shouldn't work. That is, Java is doing exactly what we the programmers told it to do, so if it doesn't work, it's because we told it the wrong thing. A lot of time is wasted by programmers clinging to, "But it has to work!"

(Yes, it's true that the Java compiler and runtime have bugs in them, as does all software, but the odds are overwhelming that the problem is due to our own mistakes, not a bug that we've found in Java.)

Good luck!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic