1

A bug in a program I'm writing as an assignment for my java. Objective: program should take two parameters and find all the odd numbers between those parameters. Then it should return the sum of all those numbers.

Bug: When the parameters are negative or starting number is greater than the ending number, it should return -1. But my program is returning 0.

Maybe my program isn't updating the sum value inside the loop. But even so, as per return condition, my function should return -1, not the sum.

public class SumOddRange{
    public static void main(String[] args){
        System.out.println(sumOdd(10,5));
    }
    
    public static boolean isOdd(int number){
        return (number<0)||(number%2==0)?false:true;
    }
    
    public static int sumOdd(int start, int end){

        int sum = 0;
        for(int i=start; i<=end; i++){
            if(isOdd(i)){
                sum+=i;
            }
        }
        return (start<=0)&&(start>end)? -1: sum;
    }
}
6
  • 1
    change your && to || -> logical OR instead of logical AND Commented Sep 10, 2020 at 10:22
  • Also, you probably need to do your input check before starting the loop, returning -1 before even getting to the loop if(start < 0 || end < 0 || start > end) return -1; Commented Sep 10, 2020 at 10:25
  • And honestly: you accepted the "wrong" answer. That answer gave you the one line you needed to "fix" your problem. Whereas the other one told so you much more ... Commented Sep 10, 2020 at 11:09
  • Omg. I just thought he is not as experienced as luk2302. So accepting his will give some appreciation or something. Commented Sep 10, 2020 at 11:26
  • I understand that i need to work more on my skills but I'm new to programming and learning. Isn't this all about learning? Thank you so much for suggesting debuggers I just googled what they are. Commented Sep 10, 2020 at 11:27

2 Answers 2

3

Put the input check as the first thing in your method and change the && to an || since you want to return if the first check fails or the second check fails, not only if both fail. And the isOdd can be inlined:

public static int sumOdd(int start, int end){
    if (start <= 0 || start > end)
        return -1;

    int sum = 0;
    for (int i = start; i <= end; i++) {
        if (i % 2 != 0) { // or if (i % 2 == 1) {
            sum += i;
        }
    }
    return sum;
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is a logic error in your line return (start<=0)&&(start>end)? -1: sum;

You have to return -1 if start is <0 or start > end, so use "||" (logical Or) instead of "&&" (logical and):

return (start<=0)||(start>end)? -1: sum;

Please go through @luk2302's answer for a better solution.

4 Comments

Hint: those checks are meant to prevent to loop on invalid numbers. What does it help to check for that AFTER the loop?!
@GhostCat, yes I agree, Ideally he should have checked for invalid inputs first and return -1, if any. Although in this case it gives the right result, it's not efficient.
And honestly, the OP accepted the wrong answer. You just fix his one problem, the other answer does much more than that. But it seems the OP was mainly worried about fixing stuff, not learning something ...
Hey how about you appreciate him for helping. Instead of being toxic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.