2016-11-12 43 views
0

我知道從我的文件中讀取的數據被解析並正確讀入,但是當我嘗試將其添加到我的arraylistCTARoute對象中時,我得到一個ArrayIndexOutOfBoundsException嘗試從顯然不存在的索引調用get方法。如何從文件中讀取數據,將對象添加到我的數組列表中?

另外,在CTARoute中的行reader = new ReadFile();似乎存在問題。

CTARoute類:

public class CTARoute{ 

     static ReadFile reader; 

     private String StationName; 
     private double Latitude; 
     private double Longitude; 
     private String Location; 
     private boolean WheelChair; 
     private int GreenLine; 
     private int RedLine; 

     public CTARoute(){ 

      StationName = ""; 
      Latitude = 0; 
      Longitude = 0; 
      Location = "elevated"; 
      WheelChair = true; 
      GreenLine = 0; 
      RedLine = 0; 

     } 

     public CTARoute(String StationName, double Latitude, double Longitude, String Location, boolean wheelChair, int GreenLine,int RedLine){ 


      this.StationName = StationName; 
      this.Latitude = Latitude; 
      this.Longitude = Longitude; 
      this.Location = Location; 
      this.WheelChair = WheelChair; 
      this.GreenLine = GreenLine; 
      this.RedLine = RedLine; 
     } 
     public String getStationName(){ 
      return StationName; 
     } 
     public Double getLatitude(){ 
      return Latitude; 
     } 
     public Double getLongitude(){ 
      return Longitude; 
     } 
     public String getLocation(){ 
      return Location; 
     } 
     public Boolean getWheelChair(){ 
      return WheelChair; 
     } 
     public int getGreenLine(){ 
      return GreenLine; 
     } 
     public int getRedLine(){ 
      return RedLine; 
     } 

     public void setStationName(String station){ 
      StationName = station; 
     } 
     public void setLatitude(double lat){ 
      Latitude = lat; 
     } 
     public void setLongitude(double lon){ 
      Longitude = lon; 
     } 
     public void setLocation(String loc){ 
      Location = loc; 
     } 
     public void setWheelChair(Boolean whe){ 
      WheelChair = whe; 
     } 

    public static void main(String args[]){ 

     Scanner scan = new Scanner(System.in); 

     reader = new ReadFile(); 
} 

的ReadFile類:

public class ReadFile { 

    ArrayList<CTARoute> route; 


    public ReadFile(){ 


    String csvFile = "CTAStops(1).csv"; 
    File file = new File(csvFile); 

    try{ 

     Scanner inputStream = new Scanner(file); 
     inputStream.nextLine(); 

     while(inputStream.hasNextLine()){ 

      route = new ArrayList<CTARoute>(); 

      String data = inputStream.nextLine(); 
      String var[] = data.split(","); 


      route.add(new CTARoute(var[0],Double.parseDouble(var[1]),Double.parseDouble(var[2]),var[3],Boolean.parseBoolean(var[4]),Integer.parseInt(var[5]),Integer.parseInt(var[6]))); 


     } 

     inputStream.close(); 

    System.out.println(route.get(2).getStationName()); //testing to see if CTARoute objects are actually added to route..... 

    }catch (FileNotFoundException e){ 

     e.printStackTrace(); 
    } 

} 

} 

回答

1

問題是與下面的代碼引起的ArrayIndexOutOfBoundsException當你CTAStops(1).csv文件中的行不包含7帶分隔符的元素,

route.add(new CTARoute(var[0],Double.parseDouble(var[1]),Double.parseDouble(var[2]),var[3],Boolean.parseBoolean(var[4]),Integer.parseInt(var[5]),Integer.parseInt(var[6]))); 

另請注意,構造函數應該只用於初始化類的實例變量(看起來here),並且在構造函數中編寫複雜的邏輯不是最好的做法(就像你在ReadFile()中做錯了那樣)。 你的代碼真的很難閱讀和維護。

相關問題