0

Basically when I am reading in my file, it does what its supposed to do up until it gets to the "Truck" line. I am getting a NumberFormatException on input String "; 1.5" for the last portion of the truck line. It does the same thing on the axles line for semi. I haven't tested it, but it probably will do the same for engine on the motorcycle portion of the file. I will paste the file I am trying to read from below my code.

Any help would be greatly appreciated. I have looked at this for a while and just cannot figure out what I am doing wrong. If I need to add any other information please let me know! Thanks everyone, in advance!

Joe

    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;

    public class UseTaxList {
       private String taxDistrict;
       private Vehicle[] vehicleList;
       private String[] excludedRecords;

       public UseTaxList() {
          taxDistrict = "not yet assigned";
          vehicleList = new Vehicle[0];
          excludedRecords = new String[0];
       }

       public void readVehicleFile(String fileIn) throws FileNotFoundException {
  vehicleList = new Vehicle[20];
  excludedRecords = new String[10];
  String owner = "";
  String yearMakeModel = "";
  double value = 0;
  boolean alt = false;
  double tons = 0;
  double engine = 0;
  int axles = 0;
  int count = 0;
  int offCount = 0;
  String fileName = fileIn;
  Scanner inFile = new Scanner(new File(fileName));
  inFile.useDelimiter(";");
  taxDistrict = inFile.nextLine().trim();
  String type = "";
  while (inFile.hasNext()) {
     type = inFile.next().toUpperCase();
     char typeChar = type.charAt(0);
     switch(typeChar) {
        case 'C': //case for Car type
           owner = inFile.next().trim();
           yearMakeModel = inFile.next().trim();
           value = Double.parseDouble(inFile.next().trim());
           alt = Boolean.parseBoolean(inFile.nextLine().trim());
           Car car = new Car(owner, yearMakeModel, value, alt);
           vehicleList[count] = car;
           count++;
           break;
        case 'T': //case for Truck type
           owner = inFile.next().trim();
           yearMakeModel = inFile.next().trim();
           value = Double.parseDouble(inFile.next().trim());
           alt = Boolean.parseBoolean(inFile.next().trim());
           tons = Double.parseDouble(inFile.nextLine().trim());
           Truck truck = new Truck(owner, yearMakeModel, value, alt, tons);
           vehicleList[count] = truck;
           count++;
           break;
        case 'S': //case for Semi type
           owner = inFile.next().trim();
           yearMakeModel = inFile.next().trim();
           value = Double.parseDouble(inFile.next().trim());
           alt = Boolean.parseBoolean(inFile.next().trim());
           tons = Double.parseDouble(inFile.next().trim());
           axles = Integer.parseInt(inFile.nextLine().trim());
           SemiTractorTrailer semi = new SemiTractorTrailer(owner, yearMakeModel,
              value, alt, tons, axles);
           vehicleList[count] = semi;
           count++;
           break;
        case 'M': //case for Motorcycle type
           owner = inFile.next().trim();
           yearMakeModel = inFile.next().trim();
           value = Double.parseDouble(inFile.next().trim());
           alt = Boolean.parseBoolean(inFile.next().trim());
           engine = Double.parseDouble(inFile.nextLine().trim());
           Motorcycle moto = new Motorcycle(owner, yearMakeModel, value, alt, engine);
           vehicleList[count] = moto;
           count++;
           break;
        default: //other type
           owner = inFile.next().trim();
           yearMakeModel = inFile.next().trim();
           String Offvalue = inFile.next().trim();
           String Offalt = inFile.nextLine().trim();
           String Offobj = owner + yearMakeModel
              + Offvalue + Offalt;
           excludedRecords[offCount] = Offobj;
           offCount++;
           break;  
     }
  }
  inFile.close();
  vehicleList = Arrays.copyOf(vehicleList, count);
    }

Tax District 52

Car; Jones, Sam; 2017 Honda Accord; 22000; false

car; Jones, Jo; 2017 Honda Accord; 22000; true

race car; Zinc, Zed; 2013 Custom Hot Rod; 61000; false

Car; Smith, Pat; 2015 Mercedes-Benz Coupe; 110000; false

Car; Smith, Jack; 2015 Mercedes-Benz Coupe; 110000; true

Truck; Williams, Jo; 2012 Chevy Silverado; 30000; false; 1.5

Firetruck; Body, Abel; 2015 GMC FE250; 55000; false; 2.5

truck; Williams, Sam; 2010 Chevy Mack; 40000; true; 2.5

Semi; Williams, Pat; 2012 International Big Boy; 45000; false; 5.0; 4

Motorcycle; Brando, Marlon; 1964 Harley-Davidson Sportster; 14000; false; 750

  • 1
    The exception message tells you exactly what is going wrong: you pass string to it that can't be turned into a number. Because: the way you are splitting your strings is wrong. And hint: read about clean code. What you put up here is close to unreadable (imho) and it doesn't come as surprise that you need other people to figure how the code *you* wrote processes the line strings in order to understand where something goes wrong. – GhostCat Dec 02 '17 at 10:52
  • I would start by taking each switch statement code into its own method. Which you can then test **on its own**! Dont write 100 lines of code and test them all at once with real data. Dissect your problem in the smallest parts ... code them, and test that code with just that data that each method needs. – GhostCat Dec 02 '17 at 10:54
  • Can 1.5 not be parsed into a double? – Joseph Smith Dec 02 '17 at 20:41
  • I am also not sure of another way to split them up. I know exactly WHERE something is going wrong(I just don’t know WHY), which is under the case ‘T’ when it gets to the “tons = parseDouble......” line that’s when I get the error. The ‘C’ switch state and the default both work fine. I’m new to Java. – Joseph Smith Dec 02 '17 at 20:48
  • The string in your question starts with a ; and a space! Those two things don't belong into a number! – GhostCat Dec 02 '17 at 20:56

0 Answers0