TryBugHunt
javaOtherEasy

Editors are refused edit access

BugFind the bug and fix the code

What it should do

record looks like "name:role". canEdit(record) returns true when the role is "editor", so canEdit("alice:editor") returns true and canEdit("bob:viewer") returns false.

What happens instead

canEdit("alice:editor") returns false.

public class Main {
    static boolean canEdit(String record) {
        String[] parts = record.split(":");
        return parts[1] == "editor";
    }
}

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 mutation and copying bugs pattern

Two references to one object, so changing one changes both; shallow copies of lists.

What you'll practise

An easy Java debugging exercise about mutation and copying bugs: two references to one object, so changing one changes both; shallow copies of lists.

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)

For objects, == checks whether both sides are the same object in memory. split() creates brand new String objects, so parts[1] is a different object from the literal "editor", even though the text is identical, and == is false. String literals in your source are shared, which is why comparisons between two literals seem to work and hide the problem in quick tests. Strings built at runtime, from split, substring, user input or files, are not. Always compare text with .equals(), or "editor".equals(parts[1]) if parts[1] might be null.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs to practise

Related errors

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/1ad74e8f-d6d3-4e26-bd2f-065993f76602" title="Debugging challenge: Editors are refused edit access" 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/1ad74e8f-d6d3-4e26-bd2f-065993f76602">TryBugHunt</a></p>