This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with array home work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with array home work." Watch "Help with array home work." New topic
Author

Help with array home work.

Sora McKain
Greenhorn

Joined: Oct 22, 2012
Posts: 4
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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?


The quieter you are, the more you are able to hear.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with array home work.
 
Similar Threads
Help with Min, Max, and Average
Help with writing to a file!
Average calculator without sentinel. A little adjustment needed.
how to compare the values in arrays
Generating random numbers with given properties