Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-if-else语句返回假阴性_Java - Fatal编程技术网

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

Java-if-else语句返回假阴性,java,Java,听起来很傻,但我想建立自己的编程语言——事实上,是一种transpiler 我一直在研究它,但遇到了一个问题,我的throwerr函数被错误的参数调用 我有一个throwerr函数,用于在出现错误时为语言“抛出错误”。throwerr(“Unk_args”,firstword,linenum)的意思是 当一行中的第一个字不在alllowedFirstWords数组中时,抛出未知参数错误 它工作得很好,只是还需要一个空格。也就是说,它说: 第2行的未知参数ie第2行的未知参数(空格) 我不知道;I

听起来很傻,但我想建立自己的编程语言——事实上,是一种transpiler

我一直在研究它,但遇到了一个问题,我的throwerr函数被错误的参数调用

我有一个throwerr函数,用于在出现错误时为语言“抛出错误”。throwerr(“Unk_args”,firstword,linenum)的意思是 当一行中的第一个字不在
alllowedFirstWords
数组中时,抛出
未知参数错误

它工作得很好,只是还需要一个空格。也就是说,它说:

第2行的未知参数
ie
第2行的未知参数(空格)

我不知道;I don’我不想那样,请帮忙

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

import java.io.IOException;
导入java.nio.charset.StandardCharset;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.array;
导入java.util.stream.stream;
公共课米朗{
//这些是颜色代码
//结束
//印刷品
静态无效打印(字符串内容){
System.out.println(什么);
}
静态void exit(){
系统出口(0);
}
静态字符串修剪(字符串修剪){
字符串posterrim=toTrim.replaceAll(“\\s”和“);
返回后边缘;
}
公共静态T[]concat(T[]第一,T[]第二){
T[]result=Arrays.copyOf(first,first.length+second.length);
System.arraycopy(秒,0,结果,first.length,second.length);
返回结果;
}
公共静态布尔包含(最终T[]数组,最终T v){
如果(v==null){
for(final te:array)if(e==null)返回true;
}否则{
for(final te:array)如果(e==v | | v.equals(e))返回true;
}
返回false;
}
静态无效抛出器(字符串错误、字符串错误、整数行数){
开关(错误){
案例“Unk_Args”:
打印(“未知参数”+err_obj+”位于第“+linenum”行);
打破
}
退出();
}
//编制。。。。。。。。。。。。。。。。。。。。。。。。。。
//编制。。。。。。。。。。。。。。。。。。。。。。。。。。
//编制。。。。。。。。。。。。。。。。。。。。。。。。。。
//编制。。。。。。。。。。。。。。。。。。。。。。。。。。
私有静态字符串编译(字符串行,int-linenum){
linenum=linenum+1;
字符串[]对象类型={“画布”,“精灵”};
String[]条件={“when”,“除非”};
String[]allowedFirstWords=concat(条件,对象类型);
String[]words=line.split(“”);
字符串firstWord=words[0].replaceAll(“\\s”,“0”);
打印(第一个字);
如果(!contains(allowedFirstWords,firstWord)&&trim(firstWord)!=“”){
投掷者(“Unk_Args”,第一个单词,linenum);
}
//打印(行+“然后”);
返回“成功”;
}
//读取文件功能
私有静态字符串读取文件(字符串文件路径){
StringBuilder contentBuilder=新的StringBuilder();
try(Stream=Files.line(path.get(filePath),StandardCharsets.UTF_8)){
stream.forEach->contentBuilder.append.append(“\n”);
}捕获(IOE异常){
印刷品(
“文件”
+文件路径
+“找不到。”
+System.getProperty(“user.dir”)
+“\n”
+“文件”
+System.getProperty(“user.dir”)
+文件路径
+“不存在”);
}
返回contentBuilder.toString();
}
//让我们转到主方法!!!
公共静态void main(字符串[]args){
//大于0
如果(args.length>0){
如果(“c”。等于(args[0])){
如果(参数[1]!=null){
字符串actualFile=readFile(args[1]);
String file=actualFile.replaceAll(“\\r\\n |\\r |\\n”和“);
字符串[]eachLine=file.split(“;”);
int noOfLines=eachLine.length;
对于(int i=0;i<2;i++){
编译(每行[i],i);
}
}否则{
System.out.println(“编译什么?\n”);
}
}
//println(“命令行“+”参数为:”);
//迭代args数组并打印
//命令行参数
用于(字符串val:args){
//System.out.println(“这些是您的其他输入”+val);
}
}否则{
System.out.println(“什么?”);
}
}
}

我不得不将
file.split(“;”
更改为
file.split(\\s*;\\s*”)
您需要实际指出代码中的问题所在,没有人会为了找到一个小错误而费力地完成所有这些。猜测:
String[]eachLine=file.split(“;”)
将保留空白,这可能是导致问题的原因。您需要类似于
split(“\\s*;\\s*”)
。您尝试过使用调试器吗<代码>修剪(第一个字)!=“
将字符串与
==
进行比较不正确。
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?");
    }
  }
}