是什么原因导致“操作数类型不兼容”?错误?

发布于 2024-12-01 20:38:12 字数 746 浏览 2 评论 0原文

我正在尝试通过类实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凑诗 2024-12-08 20:38:12

这里有两个与泛型无关的严重问题。

首先,demo.ch == System.in.read() 是一个布尔表达式。 read() 的结果(int)将自动装箱为 Integer,并且将测试该对象的身份demo.ch(为null)。

我认为你想要的是赋值运算符,=。这会将 read() 结果分配给 demo.ch

下一个问题是,您似乎期望 demo.ch 是一个 Character(基于您正在使用的转换)。但是,您尝试为其分配一个 intread() 的结果)。必要时,原始类型可以“自动装箱”,也就是说,它们可以转换为像 CharacterInteger 这样的包装对象,但前提是要转换的值是可以由目标类型表示的常量表达式。这里,值是可变的,因此不能隐式执行转换。

您可以通过将 read() 结果显式转换为 char 来解决此问题,然后让自动装箱将其转换为 Character ,但这会隐藏 EOF,它由值 -1 表示。我建议改用这样的东西:

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

请注意,我们在这里根本不使用 demo,因此其类型参数的问题是无关紧要的。

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.

云之铃。 2024-12-08 20:38:12

SampleStack.ch 的类型为 EE 是由类型参数指定的对象。由于您没有指定类型参数,编译器会为您放入 Object 。如果您希望 ch 成为一个 Character,则需要 SampleStack; demo = new SampleStack(); 或在 Java 7 SampleStack; 中演示 = new SampleStack<>();.

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

孤云独去闲 2024-12-08 20:38:12

实例化 SampleStack 时,您没有提供类型参数,因此 demo.ch 的类型为 Object。这显然无法与来自 System.inint 进行比较(或分配,无论如何,我怀疑您实际上想做的事情)。

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.

怎会甘心 2024-12-08 20:38:12

当您需要 = (赋值)时,您可以使用 == (相等测试)。您实际上从未分配给 demo.ch。相等测试返回布尔值,而不是字符,因此会出现错误消息。

您还需要将 System.in.read() 的结果转换为整数中的字符(或者使用 SampleStack 或类似的东西。 )

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

寄与心 2024-12-08 20:38:12

这段代码中有几个错误:

  • 正如人们指出的那样,您正在创建一个泛型类,但您没有概括它并原始使用它,您需要:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文