create a method that can count negative numbers in an array

16,076

Solution 1

Try giving a return statement , your method is expecting a int as a return parameter. Therefore it will give compiler error.

public class negativeTest {

    public static int Negativenum (int[] array) {

        int negative = 0;
        for (int i = 0; i < array.length; i++){
            if(array[i] < 0){
                negative = negative + 1;
            }
            System.out.println(negative);

        }
      return negative;
    }

}

Ther error you are getting is because you have not declared the main function inside the class.

You have to call Negativenum from the main function.

you can do it like this :

 public static void main (String args[])
 {
 negativeTest  nt = new negativeTest();
 int [] array = new int[]{ 100,200 };  
 int count = nt.Negativenum(array);
 System.out.println(count);  // It will print **2** 
 }

Regarding your doubts you have asked in comments.

You have to return anything from function only when you want to use that use that return value from the calling function. Otherwise if you want to just print that value on console or log that value , you can easily do it in the negativeTest function and you can change the return type of this function to void.

FYI , you should not begin your classname with the lower case character.

Solution 2

Your function signature suggest a return type of int, but you aren't returning anything from the function. I suspect this is why Eclipse is suggesting you change the function signature to return void.

If you add return negative; it should avoid the notice from Eclipse.

If your intention is to simply print the count, then you should change the return type.

Solution 3

The error is because you are not returning anything from the function which is expected to return an int.

If you want the function to count the number of negative numbers and return the count so that the caller of the function gets the count, you can add an

return negative;

before the end of the function.

Alternatively if you don't want to return anything from the function and want to just print the count as part of the function call, you can change the return type of the function from int to void:

public static void Negativenum (int[] array) {
Share:
16,076
PHZE OXIDE
Author by

PHZE OXIDE

Updated on June 05, 2022

Comments

  • PHZE OXIDE
    PHZE OXIDE almost 2 years
    public class negativeTest {
    
        public static int Negativenum (int[] array) {
    
            int negative = 0;
            for (int i = 0; i < array.length; i++){
                if(array[i] < 0){
                    negative = negative + 1;
                }
                System.out.println(negative);
    
            }
    
        }
    
    }
    

    I am trying to count how many elements in array are negative. This is what i have so far. My question is: eclipse is telling me that i should return a void instead of static int? How can i do this without using void?

    I'd want to use

    public static int negativenum(int[] array){
    

    Only way i can get this working is create an array with positive and negative numbers and count them, but i want to be able to have method that does that without creating array of numbers. Can you help me?

  • Burhan Khalid
    Burhan Khalid over 11 years
    Please add an explaination to make the answer relevant.
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    Thanks ill try that, so this method will work like i am intending for it to?
  • Isaac
    Isaac over 11 years
    I wouldn't suggest to him to return anything without knowing exactly what it is that he wants to return in the first place (if at all).
  • Burhan Khalid
    Burhan Khalid over 11 years
    Well you have a slight scope problem; you will keep printing the count for each item in the array since your println is in the body of the for loop. Other than that (and the return type mismatch), it looks okay.
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    so i probably don't need the print statement if i return negative then i take it? What your saying is when ever you have a return type int the public static "return type" negativeNum (int[] array) You have to return that same time as you specified in the return method header right?
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    otherwise if you use void you don't have to ?
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    When i run that i get a error saying: Error: Main method not found in class negativeTest, please define the main method as: public static void main(String[] args)
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    When i run that i get a error saying: Error: Main method not found in class negativeTest, please define the main method as: public static void main(String[] args) – PHZE OXIDE
  • Burhan Khalid
    Burhan Khalid over 11 years
    That's because you did not return anything; change the function signature to public static void Negativenum(int[] array) { and it will work.
  • PHZE OXIDE
    PHZE OXIDE over 11 years
    if i want to just use static int .. how would use return statement in this code? I'm not trying to use void.
  • Luiggi Mendoza
    Luiggi Mendoza over 11 years
    Your code just won't compile in Java. Big lesson: Java never passes by reference, only by value.
  • vikiiii
    vikiiii over 11 years
    you have to return the variable of same type what you have declared in the method declaration.
  • Wanabrutbeer
    Wanabrutbeer over 11 years
    if you want to actually return the value, first declare the return type as int instead of void, and then once your count is finalized, just return it with the "return count;" line. If you have if clauses, switch clauses, try catch clauses, then each branch of each of those clause types have to return something or you get a compiler error. Even if you return zero, or -1, you have to return something. Your code that calls this method can have logic to look for a -1 return to know that the method had an error or whatever