I’m stuck on Stage #MG5
I’ve tried … executing 4 times so far, always getting the same error
Here are my logs:
Looks like we failed to execute tests on time.
Please try again? Let us know at hello@codecrafters.io if this keeps happening.
And here’s a snippet of my code:
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
public class Main {
private static final String[] BUILT_IN_CMDS = { "echo", "exit", "type" };
public static void main(String[] args) throws Exception {
try (Scanner scanner = new Scanner(System.in)) {
processCommands(scanner);
}
}
private static void processCommands(Scanner scanner) {
while (true) {
System.out.print("$ ");
String input = scanner.nextLine().trim();
if (input.equals("exit 0")) {
break;
}
processInput(input);
}
}
private static void processInput(String input) {
if (input.startsWith("echo"))
handleEcho(input);
else if (input.startsWith("type"))
handleType(input);
else
System.out.println(input + ": command not found");
}
private static void handleEcho(String input) {
System.out.println(input.substring(5));
}
private static void handleType(String input) {
String cmd = input.substring(5).trim();
if (isBuiltinCommand(cmd)) {
System.out.println(cmd + " is a shell builtin");
} else {
String path = getPath(cmd);
if (path != null)
System.out.println(cmd + " is " + path);
else
System.out.println(cmd + ": not found");
}
}
private static boolean isBuiltinCommand(String command) {
for (String cmd : BUILT_IN_CMDS) {
if (cmd.equals(command))
return true;
}
return false;
}
private static String getPath(String cmd) {
String pathEnv = System.getenv("PATH");
if (pathEnv == null)
return null;
for (String directory : pathEnv.split(":")) {
Path cmdPath = Path.of(directory, cmd);
if (Files.isRegularFile(cmdPath)) {
return cmdPath.toString();
}
}
return null;
}
}