Question

I'm fairly new to Java and I'm using BlueJ. I keep getting the error:

incompatible types

Now that sounds self explanatory, but I can't seem to figure out how to fix the problem. Hope someone can help me. Thank you in advance.

Here is the code to the class Program2:

import java.util.*;

public class Program2 {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        Catalog store = new Catalog(3);
        int itemnum;
        Item item;

        try {
            store.insert
              (new Music(1111, "Gold", 12.00, "Abba"));
            store.insert
              (new Movie(2222, "Mamma Mia", 16.00, "Meryl Streep"));
            store.insert
              (new Book(3333, "DaVinci Code", 8.00, "Dan Brown"));
              store.insert
            (new Music(4444, "Legend", 15.00, "Bob Marley"));
            } catch (CatalogFull exc) {
                System.out.println(exc);
            }

        //  Insert code here to perform a sequence of
        //  interactive transactions with the user.
        //  The user enters an item number and the program
        //  either displays the item or prints an error message
        //  if the item is not found.  The program terminates
        //  when the user enters zero as the item number.

        while (!item.equals("0")) {
            item = store.find(itemnum);
            if (item != null) {
                System.out.print(itemnum);
            } else {
                System.out.printf("%s was not found.%n", item);
            }
            System.out.println();
            System.out.print("Player (0 to exit)? ");
            itemnum = kbd.next(); //Error on "()"
        }
    }
}
Était-ce utile?

La solution

String is not assignable to integer

Use nextInt() since itemnum is int , where as next() returns String, Thus incompatible types.

itemnum = kbd.nextInt();

not

itemnum = kbd.next();

And see different type

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top