开发者

What causes the "Incompatible operand types" error?

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 Obje开发者_Python百科ct and int

What is wrong here ?


There are two severe problems here that have nothing to do with generics.

First, demo.ch == System.in.read() is a boolean expression. The result of read() (an int) will be auto-boxed to an Integer, and the identity of that object will be tested against demo.ch (which is null).

I think that what you want here is the assignment operator, =. This will assign the read() result to demo.ch.

The next problem is that it looks like you expect demo.ch to be a Character (based on the casts you are using). However, you are trying to assign an int (the result of read()) to it. Primitive types can be "auto-boxed" when necessary, that is, they can be converted to a wrapper object like Character or Integer, 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 a char, and then letting the auto-boxing convert it to a Character, but that would hide EOF, which is represented by a value of -1. I recommend using something like this instead:

while (true) {
  int ch = System.in.read();
  if ((ch < 0) || (ch == '\n'))
    break;
  if (!s.isFull())
    s.push((char) ch);
 }

Note that we don't use demo here at all, so the problems with its type parameter are irrelevant.


SampleStack.ch is of type E. E is an object specified by your type parameters. Since you did not specify a type parameter, the compiler puts Object in for you. If you wanted ch to be a Character, you would want SampleStack<Character> demo = new SampleStack<Character>(); or in Java 7 SampleStack<Character> demo = new SampleStack<>();.


You haven't provided a type parameter when you instantiate SampleStack, so demo.ch is of type Object. That obviously can't be compared (or assigned, which is what I suspect you actually wanted to do, anyway) from the int coming from System.in.


You have == (equality test) when you want = (assignment). You're never actually assigning to demo.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 use SampleStack<Integer>, or something like that.)


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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜