# Code failing the cat test and the single qoute level(#N16)

**URL:** <https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [December 9, 2024, 7:04am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370 "2024-12-09T07:04:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ukpagrace](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ukpagrace/32/3355_2.png) [@ukpagrace](https://forum.codecrafters.io/u/ukpagrace)\
**Post date:** [December 9, 2024, 7:04am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/1 "2024-12-09T07:04:40Z")

</div>

I’m stuck on Stage #N16  
The cat test is failing with this, don’t know what else to do.

Expected first line of output to end with ‘\n’ (newline), got “”

This is my code.

```auto

public class Main {
    public static void main(String[] args) throws Exception {
        // Uncomment this block to pass the first stage
        List<String> commands = builtins();
        while(true){
            System.out.print("$ ");

            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            
            List<String> matchList = new ArrayList<String>();
            Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
            Matcher regexMatcher = regex.matcher(input);
            while (regexMatcher.find()) {
                matchList.add(regexMatcher.group());
            }

            String command = matchList.getFirst();
            String parameter = "";
            if(matchList.size() > 2){
                for(int i = 1; i < matchList.size(); i++){
                    if(i < matchList.size() - 1){
                        parameter += matchList.get(i) + (" ");
                    }else{
                        parameter += matchList.get(i);
                    }
                }
            }else if(matchList.size() > 1){
                parameter = matchList.get(1);
            }
            
            if(!commands.contains(command) && getPath(command) != null ){
// matchList.addFirst("-c");
// matchList.addFirst("sh");

                String[] stringArray = new String[matchList.size()];
                stringArray = matchList.toArray(stringArray);
                Process process = Runtime.getRuntime().exec(String.join(" ", matchList));
                process.getInputStream().transferTo(System.out);

// System.out.println("matchlist to array" + Arrays.toString(matchList.toArray(new String[0])));
// ProcessBuilder processBuilder = new ProcessBuilder(matchList.toArray(new String[0]));
// Process process = processBuilder.start();
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//
// String line;
// System.out.println("line" + bufferedReader.readLine());
// while((line = bufferedReader.readLine())!= null){
// System.out.println(line);
// }
            }else{
                switch(command){
                    case("exit"):
                        if(parameter.equals("0")){
                            exit(0);
                        }
                        break;
                    case("type"):
                        if (input.length() <= 5) {
                            System.out.println("type: missing argument");
                            continue;
                        }
                        if(commands.contains(parameter)){
                            System.out.print(parameter + " is a shell builtin\n");
                        }else {
                            String path = getPath(parameter);
                            if (path != null) {
                                System.out.println(parameter + " is " + path);
                            } else {
                                System.out.println(parameter + ": not found");
                            }
                        }
                        break;
                    case("echo"):
                        if(parameter.startsWith("'") && parameter.endsWith("'")){
                            System.out.println(parameter.substring(1, parameter.length()-1));
                        }else{
                            System.out.println(parameter);
                        }

                        break;
                    case("pwd"):
                        System.out.println(System.getProperty("user.dir"));
                        break;
                    case("cd"):

                        Path currentWorkingDir = Paths.get(System.getProperty("user.dir"));

                        Path path = currentWorkingDir.resolve(parameter).toAbsolutePath().normalize();
                        if(parameter.equals("~")){
                            System.setProperty("user.dir", System.getenv("HOME"));
                        }
                        else if (Files.isDirectory(Path.of(path.toString()))) {
                            System.setProperty("user.dir", path.toString());
                        } else {
                            System.out.println(command + ": " + parameter + ": No such file or directory");
                        }
                        break;
                    default:
                        System.out.println(input + ": command not found");

                }
            }
        }

    }

    public static String getPath(String command){
            for(String path: System.getenv("PATH").split(":")) {
                Path file = Path.of(path, command);
                if(Files.isRegularFile(file)){
                    return file.toString();
                }
            }
            return null;
    }

    private static List<String> builtins() {
        List<String> builtins = new ArrayList<>();
        builtins.add("exit");
        builtins.add("echo");
        builtins.add("type");
        builtins.add("pwd");
        return builtins;
    }
}

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 9, 2024, 7:36am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/2 "2024-12-09T07:36:56Z")

</div>

Hi @ukpagrace, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![ukpagrace](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ukpagrace/32/3355_2.png) [@ukpagrace](https://forum.codecrafters.io/u/ukpagrace)\
**Post date:** [December 9, 2024, 8:14am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/3 "2024-12-09T08:14:47Z")

</div>

> **[GitHub - ukpagrace/shell](https://github.com/ukpagrace/shell)**
>
> Contribute to ukpagrace/shell development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 9, 2024, 8:15am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/4 "2024-12-09T08:15:49Z")

</div>

Thanks for sharing the link! I’ll take a look and get back to you by the end of the week.

---

<div class="post-metadata">

**Author:** ![ukpagrace](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ukpagrace/32/3355_2.png) [@ukpagrace](https://forum.codecrafters.io/u/ukpagrace)\
**Post date:** [December 9, 2024, 8:24am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/6 "2024-12-09T08:24:28Z")

</div>

okay, thank you

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 15, 2024, 3:11pm UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/7 "2024-12-15T15:11:28Z")

</div>

Looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

---

<div class="post-metadata">

**Author:** ![ukpagrace](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ukpagrace/32/3355_2.png) [@ukpagrace](https://forum.codecrafters.io/u/ukpagrace)\
**Post date:** [December 17, 2024, 3:35am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/9 "2024-12-17T03:35:00Z")

</div>

Yes, I finished the challenge 🙂  
The issue was, the single quote was still being passed with the argument for the execution. something like this [“cat”, “‘/tmp/foo/f 46’”, “‘/tmp/foo/f 40’”]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [December 22, 2024, 3:35am UTC](https://forum.codecrafters.io/t/code-failing-the-cat-test-and-the-single-qoute-level-n16/3370/10 "2024-12-22T03:35:36Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
