2016-11-16 108 views
0

我正在Netbeans中構建一個java項目。我得到了我的數據文件(temperature.txt),其中包含格式爲(低)|(高)的高和低溫。文件應該加載到二維數組中,然後將其打印到屏幕上。但是當我運行我的java項目時,我遇到了這個錯誤,並且完全丟失了。但我不知道如何解決這個問題。線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:1

Temperature.text:

+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 
| Day   | 1  | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 
| Temperature | 30|32 | 29|30 | 25|28 | 25|29 | 27|31 | 28|32 | 26|30 | 24|32 | 24|41 | 27|32 | 
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 

輸出:

Analysis report of the temperature reading for the past 10 days 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
    at Lab4Ex2.main(Lab4Ex2.java:48) 
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 0 seconds) 

這裏是我的代碼:

import java.util.StringTokenizer; 
import java.io.*; 

public class Exercise { 

    public static void main(String[] args) { 

     StringTokenizer tokenizer; 
     String line; 
     String file="temperature.txt"; 
     int[][] temp=new int[10][2]; 
     int sumHigh, sumLow; 
     FileReader fr=null; 
     BufferedReader br=null; 

     try 
     { 
      fr=new FileReader(file); 
      br=new BufferedReader(fr); 

      line=br.readLine(); 
      System.out.println("Analysis report of the temperature reading for the past 10 days " + line); 

      String [] content=line.split("|"); 

      for(int row=0; row<=content.length; row++) 
      { 
       //I am trying to parse the two token into integer.. 

       if(row != 0) 
       { 
        try 
        { 
         //Parse first token into integer and store in current row column 0 
         if(row % 2 != 0) 
         { 
          sumLow = Integer.parseInt(content[row]); 
          temp[row][0]=Integer.parseInt(content[row]); <---Line 48 

         } 
         //Parse second token into integer and store in current row column 0 
         else if (row % 2 == 0) 
         { 
          sumHigh = Integer.parseInt(content[row]); 
          temp[row][1]=Integer.parseInt(content[row]); 
         } 
        } 
        catch(NumberFormatException e) 
        { 
         System.out.println("The code throws an exception"); 
        } 
       } 
       System.out.println(); 

      } 
      br.close(); 
     } 

     catch(FileNotFoundException e) 
     { 
      System.out.println("The file " + file + " was not found"); 
     } 
     catch(IOException e) 
     { 
      System.out.println("Reading error"); 
     } 
     catch(NumberFormatException e) 
     { 
      System.out.println("Parsing error"); 
     } 
     finally 
     { 
      if(fr != null) 
      { 
       try 
       { 
        fr.close(); 
       } 
       catch(IOException e) 
       { 
        System.out.println("Reading error"); 
       } 
      } 
     } 


    } 

} 
+0

哪條線是48? – bradimus

+0

你應該知道'split()'使用* regex *。因此,'|'是一個特殊的字符,它告訴它尋找一個「這個或那個」的分隔符,在這種情況下是「無論什麼或沒有」。這不是你想要的。你應該用'\\ |'代替。但是除此之外,你的程序中有很多邏輯錯誤。 – RealSkeptic

+0

'row <= content.length' ...應該不僅僅是小於嗎? 'row

回答

0

我不知道你是否已經問過這個,但這裏有一個可供您嘗試的工作解決方案。你似乎掙扎的關鍵是隻有一行文件實際上有你想讀的數據。

line.split(" \\| ?") 

這給我們留下了字符串連接的形式28|32:我用下面的正則表達式拆分此行。這些高/低對中的每一對可以進一步使用管道再分裂(但要小心,你需要逃離管道,即\\|)。最後,數據可以存儲到您的數組中,並在最後輸出爲完整性檢查。

public static void main(String[] args) { 
    String line; 
    String file = "temperature.txt"; 
    int[][] temp = new int[10][2]; 
    FileReader fr = null; 
    BufferedReader br = null; 

    try 
    { 
     fr = new FileReader(file); 
     br = new BufferedReader(fr); 

     // eat the first three lines, as they don't contain data you want to use 
     br.readLine(); 
     br.readLine(); 
     br.readLine(); 
     line = br.readLine(); 
     System.out.println("Analysis report of the temperature reading for the past 10 days " + line); 

     String [] content=line.split(" \\| ?"); 
     for (int i=1; i < content.length; ++i) { 
      String[] pair = content[i].split("\\|"); 
      temp[i-1][0] = Integer.parseInt(pair[0]); 
      temp[i-1][1] = Integer.parseInt(pair[1]); 
     } 
     System.out.println(Arrays.deepToString(temp)); 

    } 
    catch (Exception e) { 
     System.out.println("An exception occurred."); 
    } 
} 
相關問題