The mistakes everyone makes in Week 2 — and how to fix them
== operator checks if two variables point to the same object in memory, not whether the text matches. The .equals() method compares the actual content character by character. Always use .equals() or .equalsIgnoreCase() when comparing Strings.0, so "Java" has indexes 0 through 3. Calling charAt(4) asks for a position that does not exist. The last valid index is always length() - 1. The same rule applies to substring() — always check your index math against the string's length.substring(start, end) returns characters from index start up to but not including index end. Think of it as: the number of characters you get is end - start. So substring(0, 3) gives you 3 characters starting at index 0.Integer.parseInt() expects a string that contains only numeric characters (and optionally a leading minus sign). If the string has letters, spaces, decimals, or any non-digit character, Java cannot convert it and throws a NumberFormatException. Always ensure your string is actually a number before parsing.Integer.parseInt() is strict — it will not ignore whitespace. Calling .trim() on the string first removes any leading and trailing spaces, making the parse succeed.private, it can only be accessed from inside that class. This is called encapsulation — one of the core principles of OOP. To read or change private fields from outside the class, use the public getter and setter methods (like getBrand() and setBrand()).Product item; only creates a reference (a label). It does not create an actual object in memory. You must use new to call the constructor and create a real object. Without new, the variable is either uninitialized (compiler error) or null (runtime NullPointerException when you try to use it).void). If you add void (or any return type), Java thinks it is a regular method that happens to share the class name. Remove the return type to make it a proper constructor.name = name just assigns the parameter to itself — the field is untouched. Use this.name to explicitly refer to the field. This is the most common cause of fields staying null after construction.<= in the condition means the loop runs when i equals 5, which is out of bounds. Always use < (strictly less than) with .length. This is one of the most common bugs in programming, called an "off-by-one error."while loop keeps running as long as its condition is true. If nothing inside the loop changes the variable in the condition, the condition never becomes false, and the loop runs forever. Always make sure something inside the loop moves you closer to the exit condition.for(...) terminates the loop's body immediately. Java reads it as: "loop 5 times, doing nothing each time." The curly braces below become a separate block that runs once. This is a very subtle bug because the code looks correct at a glance. Always check that there is no ; between the ) and the {.0. An array of length 3 has indexes 0, 1, and 2. Index 3 is one past the end. The last valid index is always length - 1. This is the same zero-indexing rule as Strings.backup = original, you copy the reference (the memory address), not the data. Both variables now point to the same array. To create an independent copy, you must copy each element into a new array.new int[7], Java fills every slot with the default value for that type: 0 for int, 0.0 for double, false for boolean, and null for objects. These slots are not "empty" — they contain real values that will affect calculations. Always be aware of which slots you have actually filled.Click any error to expand it. These are the most common mistakes from Week 2 exercises.