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

时间:2012-04-01 14:41:01

标签: java file-handling indexoutofboundsexception awt-eventqueue

从文本文件中读取时,我似乎遇到了错误。该程序应读取一行,检查第一个字符,并在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();
        }

我收到此错误

注意,第一行输出应该在那里,这就是我知道它在第一行正确操作的方式。 “测试1”也正确显示,这让我相信问题在于我的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)

2 个答案:

答案 0 :(得分:0)

 String string1 = scan.next();

scan.next()为您提供行变量中的下一个标记,该标记可能不是整行。尝试用以下内容替换有问题的子串行:

animalType = line.substring(2, 3);

答案 1 :(得分:0)

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

X

你会收到这个错误,因为声明

string1.substring(2,3)

正在尝试从字符2

开始返回一个子字符串