java - Java 中的不兼容类型。希望得到一些帮助

标签 java bluej incompatibletypeerror

我对 Java 还很陌生,我正在使用 BlueJ。我不断收到错误:

incompatible types

现在这听起来不言自明,但我似乎不知道如何解决这个问题。希望可以有人帮帮我。预先感谢您。

以下是类 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 "()"
        }
    }
}

最佳答案

字符串不能分配给整数

使用 nextInt() 因为 itemnumint ,其中 next() 返回 String ,因此类型不兼容。

itemnum = kbd.nextInt();

不是

itemnum = kbd.next();

And see different type

关于java - Java 中的不兼容类型。希望得到一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110488/

相关文章:

c - 不兼容的类型 struct* 和 struct,

java - Eclipse 4-10 64 位 : Cannot resolve SWT library

java - 检查两个元素是否相同,每个元素来自不同的字符串数组

java - JAVA中获取两个返回值

java - BlueJ Java 谜题中移动方法的条件语句

objective-c - 不兼容的指针类型将 'NSString *__strong *' 发送到类型为 'NSError * _Nullable __autoreleasing * _Nullable' 的参数

Java 返回类型与 WebCrawler.visit(Page) 不兼容

java - 删除 oneToOne 后 Spring Hibernate TransientPropertyValueException

java - 无法删除或更新父行 : a foreign key constraint fails and Entity hierarchy mustn't change

java - 如果我修改类的静态变量,它是否会被该类的先前实例共享?