Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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 为什么我的程序在读取文件时出错?_Java_File Handling_Indexoutofboundsexception_Awt Eventqueue - Fatal编程技术网

Java 为什么我的程序在读取文件时出错?

Java 为什么我的程序在读取文件时出错?,java,file-handling,indexoutofboundsexception,awt-eventqueue,Java,File Handling,Indexoutofboundsexception,Awt Eventqueue,我在读取文本文件时似乎遇到了错误。这个程序应该读取一行,检查第一个字符,并运行if语句中的相关代码。程序在第一行正常运行,并输出内容,但无法处理下一行。以下是我使用的代码: public void importStart(){ try { FileReader fr = new FileReader("src/data.txt"); BufferedReader reader = new BufferedReader(fr); Stri

我在读取文本文件时似乎遇到了错误。这个程序应该读取一行,检查第一个字符,并运行if语句中的相关代码。程序在第一行正常运行,并输出内容,但无法处理下一行。以下是我使用的代码:

public void importStart(){

    try {
        FileReader fr = new FileReader("src/data.txt");
        BufferedReader reader = new BufferedReader(fr);

        String line = reader.readLine();
        Scanner scan = null;

            while(line != null){
                scan = new Scanner(line);
                String string1 = scan.next();
                inputType = string1.charAt(0);
                if(inputType == 'S'){
                    foxCount = scan.nextInt();
                    rabbitCount = scan.nextInt();
                    dragonCount = scan.nextInt();
                    System.out.println(inputType + " "+ foxCount + " "+ rabbitCount + " "+ dragonCount);
                }
                else if(inputType == 'X'){
                    System.out.println("Test 1");
                    animalType = string1.substring(2, 3);
                    System.out.println("Test 2");
                        if(animalType == "F"){
                            step = scan.nextInt();

                        }
                        else if(animalType == "R"){
                            step = scan.nextInt();
                        }
                        else if(animalType == "D"){
                            step = scan.nextInt();
                        }
                    System.out.println(inputType + " "+ animalType + " " + step);
                }

             line = reader.readLine();

            }
            reader.close();
        }
我收到了这个错误

注意,第一行是应该在那里的输出,这是我如何知道它在第一行上正确运行的。“test1”也正确显示,这使我相信问题出在我的string1.substring实现上。这就是问题所在吗

S 74 199 15
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:         String index out of range: 3
Test 1
at java.lang.String.substring(Unknown Source)
at DataDisplayGui.importStart(DataDisplayGui.java:107)
at DataDisplayGui.actionPerformed(DataDisplayGui.java:178)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
scan.next()为您提供line变量中的下一个标记,它可能不是整行。尝试将有问题的子字符串行替换为:

animalType = line.substring(2, 3);

取决于src/data.txt文件中的内容。 如果你读的那行是

X
您将得到此错误,因为语句

string1.substring(2,3)

正在尝试返回一个子字符串,从字符2开始

哪一行引发此异常?请尝试在第107行放置断点。string1的长度可能少于2个字符。
string1.substring(2,3)