如何在使用扫描仪扫描文本文件时读取值并将其分配给字段?

问题描述 投票:0回答:1

我是 java 的新手,我一直在从事一个包含三个类的项目,这些类使用扫描仪对象从文本文件中读取数据并将它们分配给其中两个类的字段。我的问题是值被分配给一个类的字段,而不是另一个类的字段。

这些是三个类: 车辆类,它有一个 readDetails 方法,可以从文本文件中读取自己的数据。

public class Vehicle
{
    // Vehicle Name
    private String vehicleModel;
    //Code of the vehicle
    String vehicleCode;
    //Number of times the vehicle was rented
    private int timesRented;
    //To show if the vehicle is on loan or not
    private boolean onRent;
    //The cost of a vehicle to rent
    private int price;
    //Vehicle plate number
    private int vehiclePlateNum;
     //Whether the car is convertible or not.
    private boolean isConvertible;
     //the number of doors on the car.
    private int numOfDoors;
    

    
        /**
     * Constructor for objects of class Vehicle
     */
    public Vehicle()
    {
    }
    
  /**
     * Print the details of the vehicle
     */
    public void printDetails()
    {
       System.out.println("Vehicle model: " + vehicleModel + "; code: " + vehicleCode + "; timesRented: " + timesRented + "; onRent: " + onRent + "; price: " + price + "; vehiclePlateNum: " + vehiclePlateNum);
    }
   
/**
 * Read the data for a vehicle.
 * 
 * @param scanner The scanner to use to read the data.
 */
public void readDetails(Scanner scanner) {
    try {
        while (scanner.hasNextLine()) {
            String lineOfText = scanner.nextLine().trim();

            Scanner scanner2 = new Scanner(lineOfText);
            scanner2.useDelimiter(",");

            vehicleModel = scanner2.next().trim();
            vehicleCode = scanner2.next().trim();
            timesRented = Integer.parseInt(scanner2.next().trim());
            onRent = Boolean.parseBoolean(scanner2.next().trim());
            price = Integer.parseInt(scanner2.next().trim());
            vehiclePlateNum = Integer.parseInt(scanner2.next().trim());

            


            scanner2.close();

            printDetails(); // call printDetails() to print the vehicle details
        }
    } catch (NoSuchElementException e) {
        System.out.println("Error reading data: " + e.getMessage());
    }
}
}

Car 类,它是 vehicle 的一个子类。它还有一个 readDetails 方法,可以从文本文件中读取自己的数据。

public class Car extends Vehicle {
    //Whether the car is convertible or not.
    private boolean isConvertible;
    //the number of doors on the car.
    private int numOfDoors;

    public Car()
    {
        super();
      isConvertible = this.isConvertible;
      numOfDoors = this.numOfDoors;
    }

    @Override
    public void printDetails()
    {
       System.out.println("Vehicle model: " + ShowVehicleModel() + "; code: " + ShowVehicleCode() + "; timesRented: " + ShowTimesRented() + "; onRent: " + ShowOnRent() + "; price: " + ShowPrice() + "; vehiclePlateNum: " + ShowVehiclePlateNum() + "; isConvertible: " + isConvertible + "; numOfDoors: " + numOfDoors);
    }

 /**
     * Read the data.
     */
    @Override
    public void readDetails(Scanner scanner) {
        try {
            while (scanner.hasNextLine()) {
                String lineOfText = scanner.nextLine().trim();
               
                Scanner scanner2 = new Scanner(lineOfText);
                scanner2.useDelimiter(",");
               
               
               
                super.readDetails(scanner2);
                this.isConvertible = Boolean.parseBoolean(scanner2.next().trim());
                this.numOfDoors = Integer.parseInt(scanner2.next().trim());
               
               
               
               
               

                scanner2.close();
               
            }
        } catch (NoSuchElementException e) {
            System.out.println("Error reading data: " + e.getMessage());
        }
    }
}

最后是 RentShop 类,它也有一个 readDetails 方法,负责调用 Car 类的 readDetails 方法,并将值存储在数组列表中后打印。

public class RentShop
{
    // List used to store vehicles.
    public ArrayList <Vehicle> vehicleList;

    /**
     * Constructor for objects of class RentShop
     */
    public RentShop()
    {
        vehicleList = new ArrayList<Vehicle>();
    }

   /**
    * Storing vehicles.
    */
   public void storeVehicle(Vehicle vehicle) {
        vehicleList.add(vehicle);
    }

    /**
     * Printing the details about vehicle.
     */
    public void printAllVehicles() {
        for (Vehicle vehicle : vehicleList) {
            vehicle.printDetails();
        }
    }
   
/**
 * Read the data.
 */
public void readDetails() {
    // Create file dialog box to select input file
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Select Tool Data File");
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));

    int result = fileChooser.showOpenDialog(null);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();

        try {
            // set up scanner to read from file
            Scanner scanner = new Scanner(selectedFile);

            while (scanner.hasNextLine()) {
                String lineOfText = scanner.nextLine().trim();
                System.out.println(lineOfText);

                // create a second scanner to read tokens from lineOfText
                Scanner scanner2 = new Scanner(lineOfText);
                scanner2.useDelimiter(",");
                                
                   
                     

                    

                // create a Vehicle object and add to vehicleList
                Car car = new Car();
                car.readDetails(scanner2);
                vehicleList.add(car);

                // close scanner2
                scanner2.close();
            }

            // close scanner
            scanner.close();

            // print all vehicles
            printAllVehicles();
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        }
    }
}
    }

我的问题是,每当我尝试从 RentShop 类运行 readDetails 方法时,我希望它调用 Car 类中的 readDetails 方法,但它只读取和存储 Vehicle 类的数据,它最终给了我isConvertible 和 numOfDoors 的 false 和 0 值。

现在我的目标是通过调用 Car 类中的 readDetails 方法来完成这项工作,该方法又调用 Vehicle 类中的 readDetails 方法,从而打印两个类的值。文本文件格式正确,所以问题不存在。

过去三天我一直在努力解决这个问题,所以任何帮助或建议将不胜感激。 (如果解释不当,也请提前道歉。这是我第一次在这里发帖)。

编辑:这里是文本文件中的数据: 梅赛德斯-AMG,HJ7865,5,假,10900,345789,真,2 日产GT-R,MN6754,15,真,6700,574690,假,2 丰田凯美瑞,CB0054,13,真,5800,209856,真,4

我计划稍后将其他车辆添加到项目中,所以这是汽车的数据。

java java.util.scanner subclass
1个回答
0
投票

我会从

readDetails
类和
Vehicle
类中的
Car
方法中删除循环,并从行扫描仪中读取详细信息:

车辆:

public void readDetails(Scanner scanner) {
    vehicleModel = scanner.next().trim();
    vehicleCode = scanner.next().trim();
    timesRented = Integer.parseInt(scanner.next().trim());
    onRent = Boolean.parseBoolean(scanner.next().trim());
    price = Integer.parseInt(scanner.next().trim());
    vehiclePlateNum = Integer.parseInt(scanner.next().trim());
}

汽车:

@Override
public void readDetails(Scanner scanner) {
    super.readDetails(scanner);
    this.isConvertible = Boolean.parseBoolean(scanner.next().trim());
    this.numOfDoors = Integer.parseInt(scanner.next().trim());
}
© www.soinside.com 2019 - 2024. All rights reserved.