简体   繁体   English

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

[英]Incompatible Types in Java. Hope for some assistance

I'm fairly new to Java and I'm using BlueJ . 我是Java新手,我正在使用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 : 这是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 "()"
        }
    }
}

String is not assignable to integer 字符串不能分配给整数

Use nextInt() since itemnum is int , where as next() returns String , Thus incompatible types. 使用nextInt()因为itemnumint ,其中next()返回String ,因此不兼容的类型。

itemnum = kbd.nextInt();

not

itemnum = kbd.next();

And see different type 并看到不同的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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