不兼容的类型 - int 无法转换为 java.lang.string -java - bluej

incompatible types - int cannot be converted in to java.lang.string -java - bluej

因此,作为 java 作业的一部分 - 我需要进行 CarRent 项目 - 我对 java 很陌生 - 我正在使用 blueJ - 我收到一个让我困惑的错误有点 - 寻找一些指导

    import java.util.ArrayList;
    import java.util.ArrayList; 
    import java.awt.*;
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Iterator;
    import java.lang.String; 


@author Mohsin

public class CarCompany implements ActionListener{

  private JFrame frame;
  private Container contentPane;
  private JLabel descriptionLabel;
  private JTextField descriptionTextField;
  private JLabel downPayLabel;
  private JTextField downPayTextField;
  private JLabel dailyRateLabel;
  private JTextField dailyRateTextField;
  private JLabel priceLabel;
  private JTextField priceTextField;
  private JLabel yearOfRegLabel;
  private JTextField yearOfRegTextField;
  private JLabel mileageLabel;
  private JTextField mileageTextField;
  private JLabel customerNameLabel;
  private JTextField customerNameTextField;
  private JLabel dateOfHireLabel;
  private JTextField dateOfHireTextField;
  private JLabel dateOfReturnLabel;
  private JTextField dateOfReturnTextField;
  private JLabel numberOfDaysLabel;
  private JTextField numberOfDaysTextField;
  private JLabel carNumberLabel;
  private JTextField carNumberTextField;

  private JButton addCarToRentButton;
  private JButton addCarToBuyButton;
  private JButton clearButton;
  private JButton displayAllButton;
  private JButton rentCarButton;
  private JButton sellCarButton;

  private ArrayList <CAR>carList;

   public static void main(String[] args){
    CarCompany company = new CarCompany();
  }


    public CarCompany(){

    carList = new ArrayList<CAR>()

    frame = new JFrame("Car Company");
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(5,6));

    JLabel descriptionLabel = new JLabel("Description");         //(1) the 
     description
    contentPane.add(descriptionLabel);

    contentPane.add(descriptionTextField);


    JLabel downPayLabel = new JLabel("Down Payment");           //(2) the 
    down payment
    contentPane.add(downPayLabel);

    contentPane.add(downPayTextField);

    JLabel dailyRateLabel = new JLabel("Daily Rate");           //(3) the 
    daily rate
    contentPane.add(dailyRateLabel);

    contentPane.add(dailyRateTextField);

    JLabel priceLabel = new JLabel("Price");                    //(4) the 
     price
    contentPane.add(priceLabel);

    contentPane.add(priceTextField);

    JLabel yearOfRegLabel = new JLabel("Year Of Registration"); //(5) the 
    year of registration
    contentPane.add(yearOfRegLabel);

    contentPane.add(yearOfRegTextField);

    JLabel mileageLabel = new JLabel("mileage");                  //(6) the 
    mileage
    contentPane.add(mileageLabel);

    contentPane.add(mileageTextField);

    JLabel customerNameLabel = new JLabel("Customer Name");     //(7) the 
    customer name
    contentPane.add(customerNameLabel);

    contentPane.add(customerNameTextField);

    JLabel dateOfHireLabel = new JLabel("Date Of Hire");        //(8) the 
    date of hire
    contentPane.add(dateOfHireLabel);

    contentPane.add(dateOfHireTextField);

    JLabel dateOfReturnLabel = new JLabel("Date Of Return");    //(9) the 
    date of return
    contentPane.add(dateOfReturnLabel);

    contentPane.add(dateOfReturnTextField);

    JLabel numberOfDaysLabel = new JLabel("Number Of Days");    //(10) the 
    number of days
    contentPane.add(numberOfDaysLabel);

    contentPane.add(numberOfDaysTextField);

    JLabel carNumberLabel = new JLabel("Car Number");           //(11) the 
    car number (its position in an array list of cars)
    contentPane.add(carNumberLabel);

    contentPane.add(carNumberTextField);

    JButton addCarToRentButton = new JButton("Add Car To Rent"); 
    contentPane.add(addCarToRentButton);

    JButton addCarToBuyButton = new JButton("Add Car To Buy"); 
    contentPane.add(addCarToBuyButton);

    JButton rentCarButton = new JButton("Rent Car");
    contentPane.add(rentCarButton);

    JButton sellCarButton = new JButton("Sell Car");
    contentPane.add(sellCarButton);

    JButton clearButton = new JButton("Clear"); 
    contentPane.add(clearButton);

    JButton displayAllButton = new JButton("Display All Cars");
    contentPane.add(displayAllButton);

    frame.pack();
    frame.setVisible(true);


    clearButton.addActionListener(this);
    addCarToRentButton.addActionListener(this);
    addCarToBuyButton.addActionListener(this);
    displayAllButton.addActionListener(this);
    rentCarButton.addActionListener(this);
    sellCarButton.addActionListener(this);

    }


    public void addCarToRent(){

    CarToRent newCar = new CarToRent(descriptionTextField.getText(), 
    getDownPayment(),
    getDailyRate());

    carList.add(newCar);
     }



      /* Add Car To Sell

    public void addCarToSell(){  //error is here getPrice -

    CarToBuy newCar = new CarToBuy(getPrice(),getYearOfRegistration(),
    getMileage(), descriptionTextField.getText());

    carList.add(newCar);
    }


    public void rentCar(){
    CarToRent thisCar;

    thisCar = (CarToRent) carList.get(getCarNumber());

    thisCar.rentCar(customerNameTextField.getText(), 
    dateOfHireTextField.getText(), dateOfReturnTextField.getText(),
    getNumberOfDays());

    }

    /* Return a car

    public void returnCar(){
    CarToRent thisCar = (CarToRent) carList.get(getCarNumber());
    thisCar.returnCar();
    }


    public void sellCar(){
    CarToBuy thisCar = (CarToBuy) carList.get(getCarNumber());

        if (carList.size() >= getCarNumber() && getCarNumber()>=0 && thisCar 
        instanceof CarToBuy){   
        thisCar = (CarToBuy) carList.get(getCarNumber());
        thisCar.sellCar(customerNameTextField.getText());
        }
      }

     public void displayAll(){   // Displays all the information relating to 
     properties is displayed
     for (CAR thisCar: carList)
      {
        if (thisCar instanceof CarToRent){
            CarToRent carToRent =(CarToRent)thisCar;
            carToRent.displayDetails();
        }
        else if (thisCar instanceof CarToBuy){
            CarToBuy carToBuy =(CarToBuy)thisCar;
            carToBuy.displayDetails();
      }

      }
     }



    public void actionPerformed(ActionEvent event){

    String command = event.getActionCommand();

    if (command.equals("Add Car To Rent")){
        addCarToRent();
    }

    else if (command.equals("Add Car To Buy")){
        addCarToSell();
    }

    else if (command.equals("Clear")){
        clear();
    }

    else if (command.equals("Display All Cars")){
        displayAll();
    }

    else if (command.equals("Rent Car")){
        rentCar();
    }

    else if (command.equals("Sell Car")){
        sellCar();
    }

   }


    public void clear(){    // Clears text from all eleven text fields

    descriptionTextField.setText("");
    downPayTextField.setText("");
    dailyRateTextField.setText("");
    priceTextField.setText("");
    yearOfRegTextField.setText("");
    mileageTextField.setText("");
    customerNameTextField.setText("");
    dateOfHireTextField.setText("");
    dateOfReturnTextField.setText("");
    numberOfDaysTextField.setText("");
    carNumberTextField.setText("");
   }

   //*****accessors

   public int getDailyRate(){
    return Integer.parseInt(dailyRateTextField.getText());
   }

   public int getDownPayment(){
   return Integer.parseInt(downPayTextField.getText());
    }



    public int getNumberOfDays(){
    return Integer.parseInt(numberOfDaysTextField.getText());
    }

    public int getMileage(){
    return Integer.parseInt(mileageTextField.getText());
     }

    public int getYearOfRegistration(){
    return Integer.parseInt(yearOfRegTextField.getText());
    }

    public int getCarNumber(){
    return Integer.parseInt(carNumberTextField.getText());
    }

    public int getPrice(){
    return Integer.parseInt(priceTextField.getText());
    } 
    }

以防代码太长难以阅读:无法将 int 转换为 java lang.String 是悬停在 getPrice()

上时的错误
    public void addCarToSell(){
    CarToBuy newCar = new CarToBuy(getPrice(),getYearOfRegistration(),
    getMileage(), descriptionTextField.getText());

    carList.add(newCar);
}

class CarToBuy 似乎没有(可访问的)构造函数采用四个参数,其中第一个参数是 int 但第一个参数是 string .要么提供匹配的参数,要么相应地更改 CarToBuy class,具体取决于哪一方是错误的。

看不到CarToBuy的代码class但是它的构造函数的第一个参数应该是String。 getPrice() 的 return 类型是 int。 解决问题要么

  • 将 getPrice() 的 return 类型更改为字符串(并删除整数解析)
  • 或更改 CarToBuy 的构造函数以接受 int 而不是 String
  • 或将构造函数调用更改为 new CarToBuy(Integer.toString(getPrice()),getYearOfRegistration(), getMileage(), descriptionTextField.getText());