0

從文本文件讀取時,我似乎遇到錯誤。該程序應讀取一行,檢查第一個字符,並在if語句中運行相關代碼。程序對第一行運行正常,並輸出內容,但是它不能處理下一行。下面是使用代碼IM:爲什麼我的程序在從文件讀取時出錯?

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) 
+0

哪條線拋出該異常? – 2012-04-01 14:42:34

+0

嘗試在第107行放置斷點。string1長度可能小於2個字符。 – 2012-04-01 14:53:09

回答

0
String string1 = scan.next(); 

scan.next()爲您提供了行變量的下一個標記,這可能不是整條生產線。嘗試更換冒犯子串行:

animalType = line.substring(2, 3); 
+0

謝謝你,這很好! – WazZ 2012-04-01 15:16:17

0

取決於你的src/data.txt文件。 如果你讀了線爲

X 

你會因爲語句

string1.substring(2,3) 

試圖返回一個字符串得到這個錯誤,開始字符2

相關問題