为什么Character.toString没有给出错误The method toString() in the type Object is not unavailable for the arguments (int)

发布于 2025-01-17 15:55:07 字数 388 浏览 2 评论 0原文

我正在使用 Java 中的字符,并将整数转换为字符串。我从另一个项目复制了一些代码,在那里我没有收到错误,并将其粘贴到我当前的项目中。当我运行代码时,我不断得到相同的信息,“对象类型中的方法 toString() 不适用于参数 (int)”,错误。为什么我只在一个项目中出现此错误?如何修复它?

这是我的代码:

public class main {
   public static void main(String[] args) {
       int num = 115;
       String value = Character.toString(num); //<-- Error here
       System.out.println(value);
   }
}

I was working with characters in Java and I was converting a integer to a character string. I had copied some code from another project, where I was not getting the error, and pasted it into my current project. When I ran the code and I kept getting the same, The method toString() in the type Object is not applicable for the arguments (int), error. Why do I get this error in only one project and how can I fix it?

Here's my code:

public class main {
   public static void main(String[] args) {
       int num = 115;
       String value = Character.toString(num); //<-- Error here
       System.out.println(value);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

硬不硬你别怂 2025-01-24 15:55:07

有一个 Character.toString(int) 在 Java 11 中引入,它接受一个代码点。换句话说,您可能在一个项目中使用 Java 11 或更高版本,而在另一个项目中没有使用,或者您正在针对 -release 8 或低于 11 的其他版本进行编译。

顺便说一句,您假设您正在“将整数转换为字符串”。这不是 Character.toString(int) 所做的(它将整数 Unicode 代码点转换为表示该代码点的一两个字符串)。如果要将整数转换为字符串,请使用 Integer.toString(int)<代码>String.valueOf(int)

There is a Character.toString(int) introduced in Java 11, it accepts a code point. In other words, you're probably using Java 11 or higher in one project, and not in the other, or you're compiling against -release 8 or another version below 11.

As an aside, you say you're "converting a integer to a character string". That is not what Character.toString(int) does (it converts an integer Unicode code point to a one or two character string representing that codepoint). If you want to convert an integer to string, use Integer.toString(int) or String.valueOf(int).

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