简体   繁体   中英

JAVA - won't compile

My driver class won't compile. It keeps saying that I'm giving the wrong types. I have declared the variables in the Phone class and then created information about the phone in the driver and want to store it in the array.

Phone class

public class Phone extends Technology {
    private String name;
    private String type;
    private String colour;
    private int inStock=10;
    private int sold;

    //Constructor
    public Phone (String itemsId,  String brand, double price, String name, String type, String colour ) {

        super(itemsId, brand, price );
        this.name = name;
        this.type = type;
        this.colour = colour;
    }   
}

This is the part of the driver class that wont compile, I cant figure out what im doing wrong. I have created information about the phone in the same order that they are in the phone class.

// Creating 10 phones and storing in arrayList
public void pickPhone(){

    Phone phone = new Phone("A223","Apple", "€679.00 ", "iPhone 5s", "Smartphone ", " Black");
    phoneList.add(phone);

    phone = new Phone("A252","Apple", " €649.00 ", " iPhone 5s", "Smartphone ", " White");
    phoneList.add(phone);

    phone = new Phone("A264","Apple", " €329.00 ", " iPhone 4s", "Smartphone ", " Black");
    phoneList.add(phone);

    phone = new Phone("S586","Sony", " €570.00 ", " Xperia Z1", "Smartphone ", " Black");
    phoneList.add(phone);

    phone = new Phone("S549","Sony", " €260.00 ", " Xperia SP", "Smartphone ", " Black");
    phoneList.add(phone);

    phone = new Phone("G359","Samsung", " €530.00 ", "Galaxy S4", "Smartphone ", " Black");
    phoneList.add(phone);

    phone = new Phone("G375","Samsung", " €530.00 ", "Galaxy S4", "Smartphone ", "White");
    phoneList.add(phone);

    phone = new Phone("G352","Samsung", "  €350.00 ", "Galaxy S4 Mini", "Smartphone ", "White");
    phoneList.add(phone);

    phone = new Phone("H488"," HTC ", " €529.00 ", "One", "Smartphone ", "Black");
    phoneList.add(phone);

    phone = new Phone("H463"," HTC ", " €419.00 ", "One Mini ", "Smartphone ", "Silver");
    phoneList.add(phone);

}

In your constructor

public Phone (String itemsId,  String brand, double price, String name, String type, String colour ) 

price is a double, but you pass a string eg " €419.00 "

You would have to pass the price as eg 419.0 (no Euro sign, no spaces, no double-quotes around it).

Phone phone = new Phone("A223","Apple", 679.0, "iPhone 5s", "Smartphone ", " Black");
phoneList.add(phone);

your mistake is for the price you ask for a double not a string in your constructor of phone

For example "€679.00" is a string. if you want a double you have to pass 679.00

you have to modify your constructor or to modify your argument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM