强制java以用户输入作为消息进行编译错误

问题描述 投票:-2回答:5

我不知道java中是否可能;我想从用户(来自System.in)获取输入,然后使用该输入抛出编译错误。我的意思是如果用户输入文本“HELLO”,那么程序应该抛出编译错误:编译错误:HELLO。我想要一个错误,实际上使程序在那一点上停止执行该消息。

这可能吗?如果是,怎么样?

实际上我想在运行时发生编译错误!

代码将是这样的:

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
String str = in.nextLine();

//compileError(str); this part must be completed


    }
}

这可能吗?

java compiler-errors
5个回答
1
投票

您可以让程序在此时停止执行。例如:

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        if (str.equals("HELLO")) {
            System.out.println("Compile Error: " + str);
            System.exit(1);
        }
        // Do something else.
    }
}

另一种方法是抛出一个异常...除了你将获得异常堆栈跟踪以及消息。在你正在这样做的背景下,它可能并不重要。

请注意,这有一些潜在的问题:

  1. 如果判断拦截所有输出,则可能无法获取应用程序生成的消息。
  2. 每次评判时,法官可以向您的申请提供不同的输入。

但是,这不是一个真正的编译错误。实际上,您的程序生成真正的编译错误是没有意义的。

编译代码时会发生编译错误,编译器会输出它们。但是Java应用程序在编译时无法读取输入(来自用户或“判断”)。它可以在运行时读取输入...但是编译错误为时已晚。

你似乎把你的术语搞糊涂了。我建议你阅读这些文章,了解各种术语的含义:


您的评论如下:

认为你有一个程序,你不知道它应该做什么,它通过在线评判的一些测试。你知道输入是什么,但不知道它们来自哪个顺序你应该得到测试用例而不实际访问在线评判的测试用例。我想让我的程序给我一些错误,以找出每个测试用例中的内容。

我只是有一个工作程序,想要从在线评判中提取输入。

这不是你所说的编译错误。这是一个运行时错误,因为您希望/需要它在您的程序运行时发生。有关这些条款的说明,请参阅上面的链接。


3
投票

你所指的是异常而不是编译错误。编译错误是指代码语法出现问题而Java编译器无法为您的程序生成字节代码以供JVM执行。这样就可以在你运行程序之前发生。

因此,当RuntimeException用作输入时,您可以在Java中使用"HELLO"或其任何子类的实例来终止您的程序,在我的示例代码中,如果输入是InputMismatchException,则抛出"HELLO",此异常是子类RuntimeException或未经检查的异常不要求程序员添加throws子句或明确处理它:

public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String str = in.nextLine();
      if("HELLO".equals(str)){
         throw new InputMismatchException("This input is not allowed");
     }
}

1
投票

如果您打算打印消息并退出程序,则可以执行以下操作

...
System.err.println("Your input has resulted in an error, program will terminate");
/* You can change the above text with whatever you want */
System.exit(1);

或者,你总是可以抛出一个Exception对象(或者从它派生的类的对象)并且当程序出现泡沫时不会在程序中的任何地方捕获它,程序将终止。

/* add throws Exception in your main method */
throw new Exception("Your input has resulted in an error, program will terminate")

但是,目前尚不清楚为什么要专门寻找抛出编译错误。编译程序时编译器会抛出编译时错误。在执行期间,你既不期望它们也不试图抛出它们。


1
投票

执行代码时,不能抛出编译错误。我认为您的意思是运行时错误导致您的代码在运行时失败。

尝试这样的事情:

throw new RuntimeException(str);

如果用try / catch子句包围代码,也可以捕获此“错误”。

try {
   // get the input
   throw new RuntimeException(str);
} catch (RuntimeException ex) {
   // ex contains the error details
   // ex.getMessage() will give you the user's input
}

RuntimeException是运行时错误的最常见异常,您可以使用其他异常,list of exceptions


0
投票

是的,这可以通过抛出Exception类来实现。以下是对代码的简单补充,可以解决您的问题。

public class Main {

    public static void main(String[] args) {

        try {
            Scanner in = new Scanner(System.in);
            String str = in.nextLine();
            throw new Exception(str);
        }

        catch (Exception e) {
            System.err.println(e.getMessage);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.