Touchpad Touchpad - 11 days ago 7
Java Question

Importing data from file into an int array that's called from a different class

I am working on a program that deals with inheritance. Below is one of the class that is extended from my super class bus.

public BusTrip(){
super();
int totalPassenger = 0;
fare = new int [3];
numOfPassenger = new int [3];
String destination = "";
}


I am having trouble in my driver class to import data into my ArrayList. I understand that it's reading everything in the data file position 4 all the way to the end of to the string. I have tried a while loop as well but that does not seem to go past the first set of data. Is tokenizer the only method? Is there a way for me to prevent the for loop from reading pass a certain point?

Data File

cr, greyhound, 2015, 30, 22, 44, 14, 10, 5, 15, New York

public static void readAllBus(ArrayList<Bus> busInformation){
File infile = null;
Scanner scan = null;
int[] fare = new int [3];
int[] numOfPassenger = new int [3];

try{
infile = new File("busData.csv");
scan = new Scanner(infile);
}//end try
catch(FileNotFoundException e){
System.out.println("Error! File not found!");
System.exit(0);
}//end catch

while(scan.hasNext()){
String [] str = scan.nextLine().split(",");

if(str[0].equals("cr")){
for(int i = 0; i < fare.length; i++)
for(int j = 4; j < str.length; j++)
fare[i] = Integer.parseInt(str[j]);
//error For input string: "Bahamas"
for(int i = 0; i < numOfPassenger.length; i++)
for(int j = 7; j < str.length; j++)
numOfPassenger[i] = Integer.parseInt(str[j]);
//error For input string: "Bahamas"
busInformation.add(new BusTrip(str[1],
Integer.parseInt(str[2]),
Integer.parseInt(str[3]),
fare,
numOfPassenger,
str[10]));

}//end if
}//end while
}//end readAllBus

Answer

I think you have a bug in the for loop. You are executing the inner loop multiple times when you just wanted to read the value only once. You could rewrite your logic as follows -

public static void readAllBus(ArrayList<Bus> busInformation){
  File infile = null;
  Scanner scan = null;
  int[] fare = new int [3];
  int[] numOfPassenger = new int [3];

  try{
     infile = new File("busData.csv");
     scan = new Scanner(infile);
  }//end try
  catch(FileNotFoundException e){
     System.out.println("Error! File not found!");
     System.exit(0);
  }//end catch

  while(scan.hasNext()){
     String [] str = scan.nextLine().split(",");

     if(str[0].equals("cr")){

     if(str.length>7){
          for(int i = 0; i < 3; i++){
          fare[i] = Integer.parseInt(str[i+4]);
          }
      }

      if(str.length>10){
        for(int i = 0; i < 3; i++){
          numOfPassenger[i] = Integer.parseInt(str[i+7]);
          }
       }

      busInformation.add(new BusTrip(str[1],
                           Integer.parseInt(str[2]),
                           Integer.parseInt(str[3]),
                           fare,
                           numOfPassenger,
                           str[10]));

    }//end if
  }//end while    
 }//end readAllBus