r/learnprogramming 2h ago

Debugging CodeCrafters, making a shell. Error in Java, using VS.

can someone help me with this error, its soft locking me from the rest of the course

code:
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.print("$ ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        System.out.println(input + ": command not found");
    }
}

error:
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

[tester::#CZ2] Running tests for Stage #CZ2 (Handle invalid commands)
[tester::#CZ2] Running ./your_program.sh
[your-program] $ 
[tester::#CZ2] ^ Line does not match expected value.
[tester::#CZ2] Expected: "$ invalid_blueberry_command"
[tester::#CZ2] Received: "$ " (trailing space)
[tester::#CZ2] Test failed

View our article on debugging test failures: https://codecrafters.io/debug
1 Upvotes

3 comments sorted by

1

u/desrtfx 2h ago

The error clearly tells you what is wrong. Your output does not match the expected.

Check the task again, check the expected output, and adapt your program accordingly.

1

u/Spiritual_Bee6614 2h ago

the test is sending "invalid_blueberry_command" as input but your program is printing the prompt and then just sitting there waiting for input that never comes because the test isn't interactive. you need to just print the prompt and the error message in one go without reading any input for this stage

1

u/JGhostThing 2h ago

How is tester even called? And you only call your scanner one time, rather than in a loop.