0

Trying to use conditionals instead of if statements for double comparison. Debugged step by step several times and can not figure out on what planet java thinks -9==0. When you highlight it, it says false but increases the "zero" double anyways.. The other ones seem to come come across just fine.

My input is:

6
-4 3 -9 0 4 1  

The code:

public class HRWPlusMinus {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int length = Integer.valueOf(in.nextLine());
    String[] input = in.nextLine().split(" ");
        solveWOF(length, input);
    }

    //solve with for loop
    public static void solve(int length, String[] input) {
        double pos = 0;
        double neg = 0;
        double zero = 0;
        for (int i = 0; i < length; i++){
            int test = Integer.valueOf(input[i]);
            if (test > 0) {pos++;}
            else if (test < 0) {neg++;}
            else {zero++;}
        }
        System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
    }

    //solve with conditionals
    public static void solveWOF(Integer length, String[] input) {
        double pos = 0, neg = 0, zero = 0;
        for (int i=0; i<length; i++) {
            Double test = Double.valueOf(input[i]);
            zero = (test == 0)? zero++:
            test > 0 ? pos++: neg++;
        }
        System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
    }
}

Top method works, bottom one is the one I'm having issues with.

  • In the `solveWOF` method you're checking if `test==0`, not `test < 0`. Then you check (not an `else` check, it will always be done) if the `test > 0`. – billoot Jul 12 '17 at 19:30
  • 1
    Java is not wrong.... Never assume that the JVM cannot compute correctly. – JFPicard Jul 12 '17 at 19:30
  • First conditional checks if it is zero, if false, second checks if it is greater than zero. – ArcSoftware Jul 12 '17 at 19:31
  • Had it as an int but changed it trying to troubleshoot. Didn't make a difference. Also had pos, neg and zero as ints too. – ArcSoftware Jul 12 '17 at 19:34

3 Answers3

1

Your first problem is that zero++ doesn't behave how you think it does. It retrieves the value and then increments it, so you're always assigning the value to itself. You can verify that the following prints zero:

int zero = 0;
zero = zero++;
System.out.println(zero);

Thus, this line won't work:

zero = (test == 0)? zero++:
        test > 0 ? pos++: neg++;

Some of the other problems have already been discussed in the comments.

  • Yes, I'm counting how many zeros, positives and negative numbers are in my string array. – ArcSoftware Jul 12 '17 at 19:40
  • 1
    @ArcSoftware Inspect your code - you have several lines that aren't doing what you think they're doing. For example, several of your assignments won't do anything - e.g. `zero = zero++` will literally do absolutely nothing. You can verify that fact by running the code I supplied in my answer. – EJoshuaS - Stand with Ukraine Jul 12 '17 at 19:43
  • Have also tried zero += test == 0 ? 1: test > 0 ? pos++: neg++; – ArcSoftware Jul 12 '17 at 19:44
  • 2
    @ArcSoftware Why would you want to add `pos` to `zero`? Also, this has a similar issue to what I describe above - you're incrementing *after* you do the assignment, not beforehand. Again, this isn't doing what you think it's doing. – EJoshuaS - Stand with Ukraine Jul 12 '17 at 19:46
  • 1
    @ArcSoftware The value of `someInt++` is just `someInt` - that is, the _value_ of `something++` as an expression is unchanged. @EJoshuaS has pointed out multiple times that `something = something++` does _absolutely nothing_. – Salem Jul 12 '17 at 20:03
0

I know that normally a question should not be answered with another, but what are you trying to do with this

zero = (test == 0)? zero++:
        test > 0 ? pos++: neg++;

I mean why are you assigning to zero?

Explaining:

if test is zero, this reduces to:

zero = zero++;

which is equivalent to:

int tmp = zero;    // internal variable
zero = zero + 1;
zero = tmp;

that is nothing changed (as explained by EJoshuaS).

If test is positive (or negative) it reduces to:

zero = pos++;  // or neg++ if test is < 0

that is, zero will be set to the number of positives (or negatives) so far! That is not wanted, the assignment is wrong in all cases!

Use an if as in the other method solve().

Be aware that comparison with doubles can be tricky, see:

  1. What's wrong with using == to compare floats in Java?
  2. comparing float/double values using == operator
user85421
  • 28,957
  • 10
  • 64
  • 87
0
double pos = 0, neg = 0, zero = 0;
        for (int i=0; i<length; i++) {
            Double test = Double.valueOf(input[i]);
            pos += (test > 0) ? 1:0;
            neg += (test < 0) ? 1:0;
            zero += (test == 0) ? 1:0;
        }

Fixed it, thank you. As suggested I realized that I assigning something that wasn't an assignment.