Java中的不兼容类型。希望得到一些帮助
问题描述:
我是Java
的新手,我使用的是BlueJ
。我不断收到错误:Java中的不兼容类型。希望得到一些帮助
incompatible types
现在,这听起来很明显,但我似乎无法弄清楚如何解决问题。希望可以有人帮帮我。先谢谢你。
下面是代码到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 "()"
}
}
}
答
字符串不是分配给整数
使用nextInt()
因为itemnum
是int
,其中作为next()
返回String
,因此不兼容的类型。
itemnum = kbd.nextInt();
不
itemnum = kbd.next();
+0
我会将第一行更改为“字符串不是*可分配给*整数”,因为在Java中相等性具有特定含义(即Object.equals()),这与此处不相关。 –
+1
@AndrzejDoyle Yup。编辑:)不当选择的话:( –
提示:看看在'Scanner.next()'的文件,并且变量的类型你想分配值。现在在'Scanner'上寻找其他方法,可以帮助你更多... –
在什么情况下会出现这个错误(即你在做什么来触发它)?是否还有其他与之相关的错误,例如行号或堆栈跟踪? –
如果你告诉我们错误发生在哪一行,这将有所帮助。 –