#New to java and stuck on this problem

1 messages ยท Page 1 of 1 (latest)

plain ridgeBOT
#

<@&987246399047479336> please have a look, thanks.

plain ridgeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

em.out.println(total);
}

    double average = total / numbers.length;

    return average;
}

The problem is that when I run the code, it prints the correct value for `total`, but the value of `average` is always 0. I'm not sure what I'm doing wrong. Can someone help me out?
feral iris
#

Because total and numbers.length are both ints then the result will come as int

#

you have to cast one of denominator or numerator to double or any decimal type

short estuary
#

even though ive set average to double?

short estuary
#

btw ive changed it and it now works, thanks :)

feral iris
# short estuary would you be able to explain this more? in my mind it shouldve just worked since...

In the statement double average = total / (numbers.length);, it may appear at first that average will store a value with a decimal part, like 30.2. However, there's a specific order in which Java evaluates this expression, and this order can result in unexpected behavior.

First, Java evaluates the expression within the parentheses: total / (numbers.length). Both total and numbers.length are integers, so this expression is considered an integer division. In this context, Java doesn't yet cast the result to a double.

The integer division total / (numbers.length) is executed, resulting in an integer value. In this case, it will be 30 (assuming total is 61 and numbers.length is 2).

Only after the integer division occurs does Java cast the result to a double. However, at this point, the decimal part has already been lost. Therefore, average ends up storing the value 30.0.

To avoid this loss of the decimal part, you should cast at least one of the operands to a double or float before performing the division. This way, the division is treated as a floating-point operation from the start, and the result retains the decimal part. Here's the modified code:

double average = total / (double)(numbers.length);

In this corrected version, the casting operation occurs before the division, ensuring that average is calculated as a floating-point number, resulting in a value like 30.2 (assuming the same input values).

#

ChatGPT really saves a ton of time

plain ridgeBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you ๐Ÿ‘