繁体   English   中英

如何从文件读取的数据中将对象添加到阵列列表中?

[英]How do I add an object to my arraylist from data read from a file?

我知道从我的文件中读取的数据进行解析,并正确读,但是当我试图把它添加到我arraylistCTARoute对象我得到一个ArrayIndexOutOfBoundsException的试图调用从,显然不存在索引的get方法。

另外,行reader = new ReadFile();似乎存在问题reader = new ReadFile(); CTARoute

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();
     }  

 }

}   

问题在于以下代码,当CTAStops(1).csv文件中的CTAStops(1).csv不包含7个带有定界符的元素时,将导致ArrayIndexOutOfBoundsException

 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])));

另外,请注意,构造函数仅应用于初始化类的实例变量(请参见此处 ),并且不是在构造函数内部编写复杂逻辑的最佳实践(例如您在ReadFile()中的做法是错误的)。 您的代码确实很难阅读和维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM