TryBugHunt
javaType errorEasy

Exact payment of 0.30 is flagged as short

BugFind the bug and fix the code

What it should do

paidInFull(prices, paid) returns true when the amount paid equals the cart total, so paidInFull([0.1, 0.2], 0.3) returns true and paidInFull([1.0, 2.0], 5.0) returns false.

What happens instead

paidInFull([0.1, 0.2], 0.3) returns false. paidInFull([1.5, 2.5], 4.0) returns true.

public class Main {
    static boolean paidInFull(double[] prices, double paid) {
        double total = 0;
        for (double p : prices) {
            total += p;
        }
        return total == paid;
    }
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the type errors pattern

Integer division, autoboxing surprises, and == comparing references instead of values.

What you'll practise

An easy Java debugging exercise about type errors: integer division, autoboxing surprises, and == comparing references instead of values.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

double stores numbers in binary, and most decimal fractions such as 0.1 and 0.2 have no exact binary form. Their sum is 0.30000000000000004, while the literal 0.3 is stored as a slightly different value, so == says they are different. Values like 1.5 and 2.5 are exact in binary, which is why some carts work. Never compare computed doubles with ==. Compare with a tolerance smaller than the smallest real difference, such as half a cent here. Better still for money, store whole cents in an int or long, or use BigDecimal, so the problem cannot happen.

The pattern behind it: type errors.

More type errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/b3d1fba0-5f59-4d14-a34a-6943a1cb6198" title="Debugging challenge: Exact payment of 0.30 is flagged as short" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/b3d1fba0-5f59-4d14-a34a-6943a1cb6198">TryBugHunt</a></p>