14 Replies Latest reply: Jan 31, 2007 12:05 PM by 807599 RSS

    at end of tether!  Reading in variables from a text file

    807599
      Hi all

      My stress factor has gone through the roof because I am trying to read in from a text file (you may have seen some earlir questions about ArrayLists) it's just not working!

      The code is below. The result is that it's reading in all of the car data, none of the motorbike and only the first line for the services. It's odd and it's driving me insane!

      Here's an example of the data it's reading. There are about 7-10 sets of data per type
      <car><reg>AB04CDE</reg><make>Ford</make><model>Fiesta</model><colour>blue</colour><passenger_no>4</passenger_no></car>
      <service><service_no>13570</service_no><reg>J605PLE</reg><date>15:07:2006</date><miles>20000</miles><part_replaced>brake_pads</part_replaced><part_replaced>front_tyres</part_replaced></service>
      <motorbike><reg>TT05EKJ</reg><make>Triumph</make><model>Speedmaster</model><colour>black</colour><load>20.50</load></motorbike>

      Here's the code
       while (moreToRead) {
              String line;
              try {
                line = fileReader.getNextStructure();
                
        
      // collect the data from the file
                if (line.indexOf("<car>")> -1){
                  // Select/Extract the registration element
                  int nStart = line.indexOf("<reg>");
                  int nEnd = line.indexOf("</reg>");
                  String reg = line.substring(nStart+5,nEnd);
                  
                  // Select/Extract the make element
                  nStart = line.indexOf("<make>");
                  nEnd = line.indexOf("</make>");
                  String make = line.substring(nStart+6,nEnd);
                  
                  // Select/Extract the model element
                  nStart = line.indexOf("<model>");
                  nEnd = line.indexOf("</model>");
                  String model = line.substring(nStart+7,nEnd);
                  
                  // Select/Extract the colour element
                  nStart = line.indexOf("<colour>");
                  nEnd = line.indexOf("</colour>");
                  String colour = line.substring(nStart+8,nEnd);
                  
                  // Select/Extract the passenger_no element
                  nStart = line.indexOf("<passenger_no>");
                  nEnd = line.indexOf("</passenger_no>");
                  String passenger_no = line.substring(nStart+14,nEnd);
                  
                  
                  //convert string to int
                  int passengerInt = Integer.parseInt(passenger_no);
                  
                  // declare new object car and assign the variables then add it to the array.
                  Car c = new Car (reg, make, model, colour, passengerInt);
                  carList.add(c);
            
        } else if (line.indexOf("<bike>")> -1) {
      
               // Select/Extract the registration element
               int nStart = line.indexOf("<reg>");
               int nEnd = line.indexOf("</reg>");
               String reg = line.substring(nStart+5,nEnd);
      
               // Select/Extract the make element
               nStart = line.indexOf("<make>");
               nEnd = line.indexOf("</make>");
               String make = line.substring(nStart+6,nEnd);
      
               // Select/Extract the model element
               nStart = line.indexOf("<model>");
               nEnd = line.indexOf("</model>");
               String model = line.substring(nStart+7,nEnd);
      
               // Select/Extract the colour element
               nStart = line.indexOf("<colour>");
               nEnd = line.indexOf("</colour>");
               String colour = line.substring(nStart+8,nEnd);
      
               // Select/Extract the load element
               nStart = line.indexOf("<load>");
               nEnd = line.indexOf("</load>");
               String load = line.substring(nStart+6,nEnd);
               
               //convert load string to double
               double bikeLoad = Double.parseDouble(load);
               
               // declare new object motorbike and assign the variables then add it to the array.
               Motorbike m = new Motorbike (reg, make, model, colour, bikeLoad);
               bikeList.add(m);
      
        } else  {
          
          // Select/Extract the service_number element
          int nStart = line.indexOf("<service_no>");
          int nEnd = line.indexOf("</service_no>");
          String service_no = line.substring(nStart+12,nEnd);
          console.println("service = " + service_no);
          
          nStart = line.indexOf("<reg>");
          nEnd = line.indexOf("</reg>");
          String reg = line.substring(nStart+5,nEnd);
          console.println("service = " + reg);
          
          nStart = line.indexOf("<date>");
          nEnd = line.indexOf("</date>");
          String date = line.substring(nStart+6,nEnd);
          console.println("service = " + date);
          
          nStart = line.indexOf("<miles>");
          nEnd = line.indexOf("</miles>");
          String miles = line.substring(nStart+7,nEnd);
          console.println("service = " + miles);
          
          nStart = line.indexOf("<part_replaced>");
          nEnd = line.indexOf("</part_replaced>");
          String part_replaced = line.substring(nStart+15,nEnd);
          console.println("service = " + part_replaced);
          
          //convert string to int
          int dateOfService = Integer.parseInt(date);
          //convert string to double
          double milesAtService = Double.parseDouble(miles);
          //convert service no to unique int
          int serviceNo = Integer.parseInt(service_no);
          // declare new object service and assign the variables then add it to the array.
          Service s = new Service (reg, part_replaced, serviceNo, dateOfService, milesAtService);
            serviceList.add(s);
        } 
              } 
       
            catch (Exception e) {
              // Run out of data
              moreToRead = false;
              
            } 
      } 
      If anyone can spy anything that could be causing this I love your advice. I simply can't see it.
      Jo