Java-if-else语句返回假阴性

时间:2019-06-14 15:07:09

标签: java

听起来很愚蠢,我想构建自己的编程语言-实际上是编译器。

我一直在努力,但是遇到一个问题,我的throwerr函数正在调用错误的参数

我有一个 throwerr 函数,该函数旨在在出现错误时为该语言“抛出错误”。throwerr("Unk_args",firstword,linenum)的含义是 只要一行中的第一个单词不在Unknown argument error数组中,就抛出alllowedFirstWords

它工作得很好,除了它也被称为空白。也就是说:

Unknown argument at line 2Unknown argument (whitespace) at line 2

我不希望发生这种情况。请帮忙。

这是我的代码(仅是必要的部分)

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;

public class mylang {

  // THESE ARE THE COLOR CODES

  // END
  // print
  static void print(String what) {
    System.out.println(what);
  }

  static void exit() {

    System.exit(0);
  }

  static String trim(String toTrim) {

    String afterTrim = toTrim.replaceAll("\\s", "");

    return afterTrim;
  }

  public static <T> T[] concat(T[] first, T[] second) {
    T[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
  }

  public static <T> boolean contains(final T[] array, final T v) {
    if (v == null) {
      for (final T e : array) if (e == null) return true;
    } else {
      for (final T e : array) if (e == v || v.equals(e)) return true;
    }

    return false;
  }

  static void throwerr(String err, String err_obj, int linenum) {

    switch (err) {
      case "Unk_Args":
        print("Unknown argument " + err_obj + " at line " + linenum);
        break;
    }

    exit();
  }

  // COMPILE..........................
  // COMPILE..........................
  // COMPILE..........................
  // COMPILE..........................
  private static String compile(String line, int linenum) {
    linenum = linenum + 1;
    String[] objTypes = {"canvas", "sprite"};
    String[] conditionals = {"when", "unless"};
    String[] allowedFirstWords = concat(conditionals, objTypes);

    String[] words = line.split(" ");
    String firstWord = words[0].replaceAll("\\s", "");
    print(firstWord);

    if (!contains(allowedFirstWords, firstWord) && trim(firstWord) != "") {
      throwerr("Unk_Args", firstWord, linenum);
    }
    // print(line +"then");
    return "Success";
  }

  // READ FILE FUNCTION
  private static String readFile(String filePath) {
    StringBuilder contentBuilder = new StringBuilder();

    try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
      stream.forEach(s -> contentBuilder.append(s).append("\n"));
    } catch (IOException e) {
      print(
          "File "
              + filePath
              + "not found."
              + System.getProperty("user.dir")
              + "\n"
              + "The file "
              + System.getProperty("user.dir")
              + filePath
              + " does not exist");
    }

    return contentBuilder.toString();
  }

  // LETS GO TO THE MAIN METHOD!!!

  public static void main(String[] args) {

    // greater than 0
    if (args.length > 0) {

      if ("c".equals(args[0])) {
        if (args[1] != null) {
          String actualFile = readFile(args[1]);

          String file = actualFile.replaceAll("\\r\\n|\\r|\\n", " ");

          String[] eachLine = file.split(";");

          int noOfLines = eachLine.length;

          for (int i = 0; i < 2; i++) {
            compile(eachLine[i], i);
          }
        } else {
          System.out.println("Compile what?\n");
        }
      }

      // System.out.println("The command line"+ " arguments are:");

      // iterating the args array and printing
      // the command line arguments
      for (String val : args) {
        // System.out.println("These are ur other inputs" + val);
      }

    } else {
      System.out.println("what?");
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我必须将file.split(";")更改为file.split("\\s*;\\s*")