簡體   English   中英

為什么從文件讀取時我的程序出現錯誤?

[英]Why does my program give an error when reading from file?

從文本文件讀取時,似乎遇到了錯誤。 該程序應讀取一行,檢查第一個字符,然后在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)
 String string1 = scan.next();

scan.next()在line變量中給您下一個標記,它可能不是整行。 嘗試將有問題的子字符串行替換為:

animalType = line.substring(2, 3);

取決於src / data.txt文件中的內容。 如果您讀的那行是

X

您會收到此錯誤,因為該語句

string1.substring(2,3)

正在嘗試返回從字符2開始的子字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM