alan
alan

Reputation: 11

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 Object and int

What is wrong here ?

Upvotes: 1

Views: 26344

Answers (5)

erickson
erickson

Reputation: 269697

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.

Upvotes: 4

George Armhold
George Armhold

Reputation: 31074

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.)

Upvotes: 1

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

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

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

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.

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

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

Upvotes: 1

Related Questions