简体   繁体   中英

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

I know that the data read from my file is parsed and read in correctly but when I try and add it to my arraylist of CTARoute objects i get an ArrayIndexOutOfBoundsException for trying to call a get method from an index that apparently doesn't exist.

Also, there seems to be an issue with the line reader = new ReadFile(); in CTARoute .

CTARoute class:

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 class:

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

 }

}   

The problem is with the below code which causes the ArrayIndexOutOfBoundsException when a line in your CTAStops(1).csv file DOES NOT contain 7 elements with the delimiter ,

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

Also, NOTE that Constructors SHOULD only be used for initializing instance variables of the class (look here ) and it is NOT best practice to write the complex logic inside constructors (like how you did in your ReadFile() which is wrong). Your code is really hard to read and maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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