java 关闭扫描器导致异常?

java closing scanner causes exceptions?

我正在制作一个程序,将指令从文本文件转换为图形。该文件包含命令和参数,例如“CIRCLE 100 200 15”。一旦扫描仪碰到一行“结束”,我需要关闭扫描仪,停止读取文件,并停止绘图。但是当我在 END 开关案例中使用 'obj.close()' 时,我得到了 InvocationTargetException、RuntimeException 和 IllegalStateException。我试图查找解决方案,但找不到适合我的解决方案。我试过使扫描器静态化,这会导致错误提示“此处不允许修饰符静态”,将其放入 try 语句中,将其移到 try 语句之外,但没有任何效果。这是代码:

public class Graphic extends Application {
    /**
     * The start method. Required by Application
     *
     * @param stage
     */
    public void start(Stage stage) {
        double fwidth = 0;
        double fheight = 0;
        ...

        Group root = new Group();  //creates group for all shapes to go in
        try {
            Scanner obj = new Scanner(new File("star.txt"));  //reads text file
            while(obj.hasNextLine()){
                String[] strArray = obj.nextLine().split(" ");  //splits all commands to new line
                switch(strArray[0]){
                    case "SIZE":                                      //sets size of window
                        ...
                    case "LINE":                                      //creates each line
                        ...
                    case "CIRCLE":                                    //creates each circle
                        ...
                    case "RECTANGLE":                                 //creates each rectangle
                        ...
                    case "TEXT":                                      //creates each string of text
                        ...
                    case "//":                                        //ignores comments
                        ...
                    case "END":                                       //stops reading file
                        obj.close();
                        break;
                }
            }

            Scene scene = new Scene(root, fwidth, fheight, Color.BLACK);
            stage.setTitle("poop");
            stage.setScene(scene);
            stage.show();
        }
        catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        }
        }

        /**
         * The main method
         * @param args
         */

        public static void main(String[] args) {
            launch(args);
        }
    }

这里是错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1070)
    at java.util.Scanner.findWithinHorizon(Scanner.java:1670)
    at java.util.Scanner.hasNextLine(Scanner.java:1500)
    at sample.Graphic.start(Graphic.java:69)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null(WinApplication.java:177)
    ... 1 more
Exception running application sample.Graphic

Process finished with exit code 1

idk 如果它是一个简单的修复,我只是错过了它或什么,但如果有人可以提供帮助,我们将不胜感激。

将循环放在 try-with-resources 块中,然后它将自行关闭扫描器,因为 Scanner 实现了 Closeable。

try (Scanner obj = new Scanner(new File("star.txt"))) {
    //Place your whole while loop here
}

您跳出了 switch/case,因此您应该在 try 中创建一个布尔值,就在您的循环上方,然后在“END”时将其设置为 true。 然后在你的循环中检查变量的值,如果它是真的,然后像这样跳出循环:

在 while 循环之上:

boolean shouldBreak = false;

结束案例:

case "END":
    shouldBreak = true;
    break;

然后在循环结束时(内部)

if(shouldBreak) break;