Question

I'm doing the code for a shop and this part is to allow the staff to add a phone to existing stock. I cant figure out what the problem is as it wont compile and says "incompatible types", it marks the part [ name= " "; type=" "; colour=" ";]

Here's the part of the driver class that it is in:

      //add new phone to stock
public void addPhone( ) {

     Phone phone = new Phone ( itemsId, brand, price, name, type, colour);

    Scanner scan = new Scanner(System.in);
    String itemsId; 
    String brand;
    double price;
    String name;
    String type;
    String colour;


    for (int counter=0; counter< 1; counter ++)
    {
        itemsId= "  ";
        brand="     ";
        price= 
        name= "  ";
        type="  ";
        colour=" ";

        System.out.println("Enter itemsID:   ");
        itemsId= scan.nextLine();

        System.out.println("Enter brand:     ");
        brand= scan.nextLine();

        System.out.println("Enter price:     ");
        price= scan.nextdouble();

        System.out.println("Enter name:      ");
        name= scan.nextLine();

        System.out.println("Enter type:       ");
        type= scan.nextLine();

        System.out.println("Enter colour:     ");
        colour= scan.nextLine();

    }
    phoneList.add(phone);

}
Was it helpful?

Solution 2

In Java statements are semi-colon terminated. Then

price= 
name= "  ";

is parsed as price = (name = " "). However, a String (type/result of the name assignment expression) is incompatible with a double (the type of the price variable) as they are "incompatible types".

So first step is to fix how the code is parsed

price= <put something useful here>;
name= "  ";

While this will "fix" the immediate type error, the program will still not compile (due to unassigned local variables and incorrect method names) and, if it did compile, would not produce the correct results when more than one phone number is to be entered - keep reading.


Now, as far as the overall program: don't assign these "dummy values" and don't use the variables before they have meaningful values.

Consider this:

Scanner scan = new Scanner(System.in);

for (int counter = 0; counter < 1; counter++)
{
    // We don't need to assign defaults because these
    // variables will be assigned useful values before
    // they are used.
    double price;
    String name;
    // (other variables removed for simplicity)

    System.out.println("Enter price:     ");
    // Java is case-sensitive, make sure to use correct case.
    // The original "nextdouble" was incorrect.
    price = scan.nextDouble();

    System.out.println("Enter name:      ");
    name = scan.nextLine();

    // NOW use variables - AFTER they have useful values
    Phone phone = new Phone (.., .., price, name, .., ..);

    // Add new phone entry each loop,
    // not once at the end of all (1) loops.
    phoneList.add(phone);
}

OTHER TIPS

You should assign a value to price.

price = 0.0;
name = " ";

should work.

price = 
name = " ";

is the same code as

price = name = " ";

which means that you're assigning String value to the double - these types are incompatible; Either, remove "price = " line or add value to the right of "price = " line:

price = 0;
name = " ";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top