Skip to content
Advertisement

not returning anything although there is no problem public static int

I got homework to do where I need to make it return 1 if sum is bigger or equals to 2 and 0 otherwise. But it’s not returning anything.

Thanks for help. Here is the program:

public static void main(String[] args) {
    Singer s1 = new Singer("yoav",40,8);
    Singer [] sArr = new Singer[9];
    String[] nameArr = new String[] {
        "Ariel", "Dana", "Eyal", "Miri", "Keren"
    };
    System.out.println(s1);
    for(int i = 0; i < sArr.length; i++) {
        int name = (int)(Math.random() * nameArr.length);
        int age = (int)(Math.random() * 20);
        int album = (int)(Math.random() * 6) + 1;
        sArr[i] = new Singer(nameArr[name], s1.getAge() -age, s1.getGoldA() - album);
        System.out.println(sArr[i]);
    }   
}

public static int twoOrLess(Singer [] s1Arr) {
    int sum = 0;
    for(int i = 0; i < s1Arr.length - 1; i++) {
        if(s1Arr[i].getGoldA() < s1Arr[i+1].getGoldA()) {
            sum = sum + 1;
        }
    }
    if(sum >= 2) {
        return 1;
    }
    else {
        return 0;
    }
}

Advertisement

Answer

You never called twoOrLess, so you just call it from your main.

twoOrLess does not get called automatically like main. You have to call twoOrLess.

EDIT

public static void main(String[] args) {
    ...
    int value = twoOrLess(); // put an array of some objects of Singer
    System.out.println(value);
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement