是什么原因导致“操作数类型不兼容”?错误?
我正在尝试通过类实现 iSortableStack 接口。
这是我的主要函数,
public class SampleStack<E> {
E ch;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
ISortableStack<Character> s = new SortableStack<Character>();
SampleStack demo = new SampleStack();
while ((demo.ch == System.in.read()) != '\n')
if (!s.isFull())
s.push((Character) demo.ch);
while (!s.isEmpty())
System.out.print(s.pop());
System.out.println();
}
}
但我在这一行遇到一个错误,
while ((demo.ch == System.in.read()) != '\n')
错误:不兼容的操作数类型对象和 int
这里出了什么问题?
I am trying to implement iSortableStack Interface via a class.
Here's my main function,
public class SampleStack<E> {
E ch;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
ISortableStack<Character> s = new SortableStack<Character>();
SampleStack demo = new SampleStack();
while ((demo.ch == System.in.read()) != '\n')
if (!s.isFull())
s.push((Character) demo.ch);
while (!s.isEmpty())
System.out.print(s.pop());
System.out.println();
}
}
But I am getting one error, on this line,
while ((demo.ch == System.in.read()) != '\n')
Error : Incompatible operand types Object and int
What is wrong here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有两个与泛型无关的严重问题。
首先,demo.ch == System.in.read() 是一个布尔表达式。
read()
的结果(int
)将自动装箱为Integer
,并且将测试该对象的身份demo.ch
(为null
)。我认为你想要的是赋值运算符,
=
。这会将read()
结果分配给demo.ch
。下一个问题是,您似乎期望
demo.ch
是一个Character
(基于您正在使用的转换)。但是,您尝试为其分配一个int
(read()
的结果)。必要时,原始类型可以“自动装箱”,也就是说,它们可以转换为像Character
或Integer
这样的包装对象,但前提是要转换的值是可以由目标类型表示的常量表达式。这里,值是可变的,因此不能隐式执行转换。您可以通过将
read()
结果显式转换为char
来解决此问题,然后让自动装箱将其转换为Character
,但这会隐藏 EOF,它由值 -1 表示。我建议改用这样的东西:请注意,我们在这里根本不使用
demo
,因此其类型参数的问题是无关紧要的。There are two severe problems here that have nothing to do with generics.
First,
demo.ch == System.in.read()
is aboolean
expression. The result ofread()
(anint
) will be auto-boxed to anInteger
, and the identity of that object will be tested againstdemo.ch
(which isnull
).I think that what you want here is the assignment operator,
=
. This will assign theread()
result todemo.ch
.The next problem is that it looks like you expect
demo.ch
to be aCharacter
(based on the casts you are using). However, you are trying to assign anint
(the result ofread()
) to it. Primitive types can be "auto-boxed" when necessary, that is, they can be converted to a wrapper object likeCharacter
orInteger
, but only when the value to be converted is a constant expression that can be represented by the target type. Here, the value is variable, so the conversion cannot be performed implicitly.You could work around this by explicitly casting the
read()
result to achar
, and then letting the auto-boxing convert it to aCharacter
, but that would hide EOF, which is represented by a value of -1. I recommend using something like this instead:Note that we don't use
demo
here at all, so the problems with its type parameter are irrelevant.SampleStack.ch
的类型为E
。E
是由类型参数指定的对象。由于您没有指定类型参数,编译器会为您放入Object
。如果您希望ch
成为一个Character
,则需要SampleStack; demo = new SampleStack();
或在 Java 7SampleStack; 中演示 = new SampleStack<>();
.SampleStack.ch
is of typeE
.E
is an object specified by your type parameters. Since you did not specify a type parameter, the compiler putsObject
in for you. If you wantedch
to be aCharacter
, you would wantSampleStack<Character> demo = new SampleStack<Character>();
or in Java 7SampleStack<Character> demo = new SampleStack<>();
.实例化
SampleStack
时,您没有提供类型参数,因此demo.ch
的类型为Object
。这显然无法与来自System.in
的int
进行比较(或分配,无论如何,我怀疑您实际上想做的事情)。You haven't provided a type parameter when you instantiate
SampleStack
, sodemo.ch
is of typeObject
. That obviously can't be compared (or assigned, which is what I suspect you actually wanted to do, anyway) from theint
coming fromSystem.in
.当您需要
=
(赋值)时,您可以使用==
(相等测试)。您实际上从未分配给demo.ch
。相等测试返回布尔值,而不是字符,因此会出现错误消息。您还需要将
System.in.read()
的结果转换为整数中的字符(或者使用SampleStack
或类似的东西。 )You have
==
(equality test) when you want=
(assignment). You're never actually assigning todemo.ch
. The equality test returns boolean, rather than char, hence the error message.You will also need to cast the result from
System.in.read()
to a character from an integer (or else useSampleStack<Integer>
, or something like that.)这段代码中有几个错误:
正如人们指出的那样,您正在创建一个泛型类,但您没有概括它并原始使用它,您需要:
SampleStack
即使你改变它,它也不会运行,因为你有 == 而不是 =
即使你改变上面两个它不会工作,因为 System.in.read() 返回一个 int,而不是一个字符,您需要创建一个整数堆栈或从输入读取值到变量,然后将其转换,但它不是很好的做法。我会使用扫描仪或类似的东西来读取用户输入的内容,如下所示:
扫描仪 sc = new Scanner(System.in);
char c = sc.nextChar();
You have several errors in this code:
as people pointed out you're making a generic class but you're not generalizing it and using it raw, you need:
SampleStack<Character>
even if you change it it wont run as you have == instead of =
even if you change the above two it wont work as System.in.read() returns an int, not a character, You'd need to either make a stack of Integers OR read the value from the input to a variable and then cast it but its not a good practice. I'd use a Scanner or somethign similar to read what the user inputs like this:
Scanner sc = new Scanner(System.in);
char c = sc.nextChar();