Java : Not able to handle new line character

I’m stuck on Scanning: String literals.

I’ve tried running my code with some own custom test cases. I got some unexpected error. My code not able to handle new line character.

Test Case:

+
+

Output

PLUS + null
[line 1] Error: Unexpected character:
PLUS + null
EOF  null

And here’s a snippet of my code:


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
  public static void main(String[] args) {

    if (args.length < 2) {
      System.err.println("Usage: ./your_program.sh tokenize <filename>");
      System.exit(1);
    }

    String command = args[0];
    String filename = args[1];

    boolean hasErrors = false;
    int lineNumber = 1;

    if (!command.equals("tokenize")) {
      System.err.println("Unknown command: " + command);
      System.exit(1);
    }

    String fileContents = "";
    try {
      fileContents = Files.readString(Path.of(filename));
    } catch (IOException e) {
      System.err.println("Error reading file: " + e.getMessage());
      System.exit(1);
    }

    if (fileContents.length() > 0) {
      for (int idx = 0; idx < fileContents.length(); idx++) {
        char c = fileContents.charAt(idx);

        // if (Character.isWhitespace(c)) continue; // Ignore whitespace characters
        // using Character Class
        if (c == ' ' || c == '\t')
          continue;

        // For every new line we will count number
        if (c == '\n') {
          ++lineNumber;
          continue;
        }

        switch (c) {
          case '(' -> System.out.println("LEFT_PAREN ( null");
          case ')' -> System.out.println("RIGHT_PAREN ) null");
          case '{' -> System.out.println("LEFT_BRACE { null");
          case '}' -> System.out.println("RIGHT_BRACE } null");
          case '.' -> System.out.println("DOT . null");
          case ',' -> System.out.println("COMMA , null");
          case ';' -> System.out.println("SEMICOLON ; null");
          case '+' -> System.out.println("PLUS + null");
          case '-' -> System.out.println("MINUS - null");
          case '*' -> System.out.println("STAR * null");
          case '=' -> {
            if (idx + 1 < fileContents.length() && fileContents.charAt(idx + 1) == '=') {
              System.out.println("EQUAL_EQUAL == null");
              ++idx;
            } else {
              System.out.println("EQUAL = null");
            }
          }
          case '!' -> {
            if (idx + 1 < fileContents.length() && fileContents.charAt(idx + 1) == '=') {
              System.out.println("BANG_EQUAL != null");
              ++idx;
            } else {
              System.out.println("BANG ! null");
            }
          }
          case '<' -> {
            if (idx + 1 < fileContents.length() && fileContents.charAt(idx + 1) == '=') {
              System.out.println("LESS_EQUAL <= null");
              ++idx;
            } else {
              System.out.println("LESS < null");
            }
          }
          case '>' -> {
            if (idx + 1 < fileContents.length() && fileContents.charAt(idx + 1) == '=') {
              System.out.println("GREATER_EQUAL >= null");
              ++idx;
            } else {
              System.out.println("GREATER > null");
            }
          }
          case '/' -> {
            if (idx + 1 < fileContents.length() && fileContents.charAt(idx + 1) == '/') {
              while (idx < fileContents.length() && fileContents.charAt(idx) != '\n') {
                idx++;
              }
              ++lineNumber;
            } else {
              System.out.println("SLASH / null");
            }
          }
          default -> {
            System.err.println("[line " + lineNumber + "] Error: Unexpected character: " + c);
            hasErrors = true;
          }
        }
      }
    }
    System.out.println("EOF  null");

    if (hasErrors) {
      System.exit(65);
    } else {
      System.exit(0);
    }
  }
}


Hi @debabrata2050, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Lox/codecrafters-interpreter-java/src/main/java/Main.class at main · debabrata2050/Lox

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

I noticed you’ve posted a new topic. Just checking – the current issue is resolved, right?

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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