← Back to Week 1 Hub

Common Errors Guide

The mistakes everyone makes in Week 1 — and how to fix them

#1 Using == to compare Strings Use .equals() instead of ==
What you wrote:
String name = scanner.nextLine(); if (name == "Alice") { System.out.println("Welcome, Alice!"); }
What happens: The condition is false even when the user types "Alice". The program skips the if-block and nothing prints.
The fix:
String name = scanner.nextLine(); if (name.equals("Alice")) { System.out.println("Welcome, Alice!"); }
Why: The == operator compares whether two variables point to the same object in memory, not whether the text is the same. The .equals() method compares the actual content of the Strings character by character. Always use .equals() (or .equalsIgnoreCase()) for String comparison.
#2 Scanner nextInt() then nextLine() skips input The leftover newline problem
What you wrote:
System.out.println("Enter your age:"); int age = scanner.nextInt(); System.out.println("Enter your name:"); String name = scanner.nextLine(); // gets skipped!
What happens: The program never waits for the name. It prints "Enter your name:" and immediately moves on. The name variable ends up as an empty string "".
The fix:
System.out.println("Enter your age:"); int age = scanner.nextInt(); scanner.nextLine(); // consume the leftover newline System.out.println("Enter your name:"); String name = scanner.nextLine(); // now works correctly
Why: When you type 25 and press Enter, the input buffer contains 25\n. The nextInt() reads only the 25 and leaves the \n (newline) sitting in the buffer. The next nextLine() sees that leftover \n and thinks it got its input. Adding an extra scanner.nextLine() right after nextInt() consumes that leftover newline.
#3 Integer division gives wrong answer 10 / 3 gives 3, not 3.333
What you wrote:
double result = 10 / 3; System.out.println(result); // prints 3.0, not 3.333...
What happens: The output is 3.0 instead of 3.333.... Java performs integer division first (10 / 3 = 3), then converts 3 to 3.0 when storing it in the double variable.
The fix:
// Option 1: Cast one operand to double double result = (double) 10 / 3; // Option 2: Make one operand a decimal literal double result = 10.0 / 3; System.out.println(result); // prints 3.3333333333333335
Why: When both operands are int, Java does integer division and truncates the decimal part. The result type of the expression is determined before it gets assigned to the variable. Making at least one operand a double forces Java to use decimal division.
#4 Forgetting break in switch statement Fall-through runs extra cases
What you wrote:
switch (day) { case 1: System.out.println("Monday"); case 2: System.out.println("Tuesday"); case 3: System.out.println("Wednesday"); }
What happens: If day is 1, the output is:
Monday
Tuesday
Wednesday
All three cases execute because of "fall-through."
The fix:
switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; }
Why: In a traditional switch statement, once a matching case is found, Java executes all code from that point downward — including other cases — unless it hits a break. This is called "fall-through." Always add break; at the end of each case unless you intentionally want fall-through behavior.
#5 Using print instead of println Output runs together on one line
What you wrote:
System.out.print("Hello"); System.out.print("World"); System.out.print("Java");
What happens: The output is all on one line with no spacing:
HelloWorldJava
Nothing separates the words, and there is no newline at the end.
The fix:
System.out.println("Hello"); System.out.println("World"); System.out.println("Java");
Why: System.out.print() outputs text without adding a newline at the end. System.out.println() does the same thing but adds a newline character after the text, so each piece of output appears on its own line. In this course, always use println() for prompts and output.
#6 Calling a method without required arguments Compiler error: missing argument
What you wrote:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); displayMenu(); // missing the argument! } public static void displayMenu(Scanner scanner) { System.out.println("Enter your choice:"); int choice = scanner.nextInt(); }
What happens: Compiler error:
java: method displayMenu in class Main cannot be applied to given types;
  required: java.util.Scanner
  found: no arguments
The fix:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); displayMenu(scanner); // pass the scanner! } public static void displayMenu(Scanner scanner) { System.out.println("Enter your choice:"); int choice = scanner.nextInt(); }
Why: When a method's signature says it requires a Scanner parameter, you must provide one when calling it. The method needs that Scanner object to read user input. Think of parameters as ingredients a recipe requires — you cannot skip them.
#7 Declaring Scanner inside every method Pass it as a parameter instead
What you wrote:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); getAge(); } public static void getAge() { Scanner scanner = new Scanner(System.in); // new Scanner here too! System.out.println("Enter your age:"); int age = scanner.nextInt(); }
What happens: The code compiles and may seem to work, but you are creating multiple Scanner objects reading from the same input stream. This can cause unpredictable behavior and is wasteful. It also makes your code harder to maintain.
The fix:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); getAge(scanner); // pass scanner to the method } public static void getAge(Scanner scanner) { System.out.println("Enter your age:"); int age = scanner.nextInt(); }
Why: Create one Scanner in main() and pass it as a parameter to any method that needs user input. This is cleaner, avoids resource conflicts, and is the preferred pattern in this course. Never re-declare new Scanner(System.in) in multiple methods.
#8 Wrong variable type for user input InputMismatchException at runtime
What you wrote:
System.out.println("Enter your age:"); int age = scanner.nextInt(); // User types: 25.5
What happens: Runtime crash:
Exception in thread "main" java.util.InputMismatchException
The program terminates immediately. This also happens if you use nextInt() and the user types a word like "hello".
The fix:
// If the input might be a decimal, use nextDouble() System.out.println("Enter your age:"); double age = scanner.nextDouble(); // Or if you want an integer, make sure prompts // clearly tell the user what format to enter System.out.println("Enter your age (whole number):"); int age = scanner.nextInt();
Why: nextInt() can only read whole numbers. If the user types a decimal like 25.5 or text like "hello", Java throws an InputMismatchException and the program crashes. Match your Scanner method to the type of input you expect: nextInt() for integers, nextDouble() for decimals, nextLine() for text.
#9 Missing return statement Method promises to return a value but doesn't
What you wrote:
public static int addNumbers(int a, int b) { int sum = a + b; System.out.println("Sum is: " + sum); // forgot to return the value! }
What happens: Compiler error:
java: missing return statement
The code will not compile at all.
The fix:
public static int addNumbers(int a, int b) { int sum = a + b; System.out.println("Sum is: " + sum); return sum; // return the value }
Why: When a method declares a return type (like int), it promises to give back a value of that type. If your method does not need to return anything, use void as the return type instead. If it does return something, every possible code path must end with a return statement.
#10 String concatenation with numbers "Score: " + 10 + 20 gives "Score: 1020"
What you wrote:
int a = 10; int b = 20; System.out.println("Score: " + a + b);
What happens: The output is Score: 1020 instead of Score: 30. Java concatenates the numbers as strings instead of adding them.
The fix:
int a = 10; int b = 20; // Option 1: Use parentheses to force addition first System.out.println("Score: " + (a + b)); // Option 2: Calculate separately int total = a + b; System.out.println("Score: " + total);
Why: Java evaluates + left to right. When it sees "Score: " + a, the String forces concatenation, producing "Score: 10". Then "Score: 10" + b concatenates again to give "Score: 1020". Wrapping (a + b) in parentheses forces Java to do the math first (because both operands are int), then concatenate the result with the String.

Click any error to expand it. These are the most common mistakes from Week 1 exercises.

← Glossary / Key Terms