BlueJ 错误:“不兼容的类型:int 无法转换为 java.lang.String”和“不兼容的类型:java.lang.String 无法转换为 int”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43801235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:44:21  来源:igfitidea点击:

BlueJ Error: "Incompatible types: int cannot be converted java.lang.String" AND "Incompatible types: java.lang.String cannot be converted to int"

javabluej

提问by Ann

I'm very new to coding and after trying multiple solutions I came up with, I still can't figure out why what I'm doing is wrong. This is my full code:

我对编码非常陌生,在尝试了多种解决方案后,我仍然无法弄清楚为什么我所做的事情是错误的。这是我的完整代码:

public class Student {
  private String name;
  private String grade;
  private String gender;
  private int number;

  public Student( String name,  String grade, String gender, int number ) {
    this.name = name;
    this.grade = grade;
    this.gender = gender;
    this.number = number;
  }

  public String getName() {
    return name;
  }

  public void setName( String name ) {
    this.name = name;
  }

  public String getGrade() {
   return grade;
  }

  public void setGrade( String grade ) {
    this.grade = grade;
  }

  public String getGender() {
    return gender;
  }

  public void setGender( String gender ) {
    this.gender = gender;
  }

  public String getNumber() {
    return number;
  }

  public void setNumber( String number ) {
    this.number = number;
  }
}

As you can probably see, I'm trying to not only be able to add a new name/grade/gender/int number when I first use an object, but also by using methods.

正如您可能看到的,我尝试不仅能够在我第一次使用对象时添加新的名称/等级/性别/整数编号,而且还尝试使用方法。

The problem I'm getting seems to be caused in this part:

我遇到的问题似乎是在这部分引起的:

public String getNumber() {
  return number;
}

public void setNumber( String number ) {
  this.number = number;
}

When I hover over "number" in the second line, BlueJ gives the following error: "Incompatible types: int cannot be converted to java.lang.String".

当我将鼠标悬停在第二行的“数字”上时,BlueJ 给出以下错误:“不兼容的类型:int 无法转换为 java.lang.String”

Though when I hover over "number" in the fifth line, BlueJ gives the error: "Incompatible types: java.lang.String cannot be converted to int".

虽然当我将鼠标悬停在第五行的“数字”上时,BlueJ 给出了错误:“不兼容的类型:java.lang.String 无法转换为 int”

I've tried searching on this website for similar problems, but haven't found any where they tried to use an int number to be filled in by the use of methods.

我试过在这个网站上搜索类似的问题,但没有找到任何他们试图使用 int 数字通过使用方法填充的地方。

采纳答案by GhostCat

String and int are twodifferent types, and in contrast to Integer and int, there is no compiler-magic to turn the one into the other automatically.

String 和 int 是两种不同的类型,与 Integer 和 int 相比,没有编译器魔术可以自动将一种转换为另一种。

But there are various helper methods to work around this, like:

但是有多种辅助方法可以解决此问题,例如:

String asString = Integer.toString(123);

which turns intinto String; and

这匝intString; 和

int number = Integer.parseInt("123"); 

for the other way round.

反过来。

And just for the record: the fact that you can write

只是为了记录:你可以写的事实

Integer integerObject = 5;

is called auto-boxing.

称为自动装箱