Java:toString 错误“错误:类型不兼容:int 无法转换为字符串”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35167196/
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
Java: toString error "error: incompatible types: int cannot be converted to String"
提问by Paincakes
I just need a quick help with my toString
. This involves playing cards.
我只需要快速帮助我的toString
. 这涉及打牌。
public class Card
{
//Data
private int rank;
private char rank2;
private char suit;
//more codes (constructor etc.)
//toString
public String toString ( )
{
if (rank == 11)
rank2 = 'J';
else if (rank == 12)
rank2 = 'Q';
else if (rank == 13)
rank2 = 'K';
else if (rank == 14)
rank2 = 'A';
else
return rank2 + suit; // <----error here.
}
I am trying to figure out how to return a rank and a suit. For example, if someone were to input 11 for the rank and 'S' for the suit, then I should return a JS. However, I am running into an error:
我想弄清楚如何归还军衔和西装。例如,如果有人要为等级输入 11,为西装输入“S”,那么我应该返回一个 JS。但是,我遇到了一个错误:
"error: incompatible types: int cannot be converted to String"
“错误:类型不兼容:int 无法转换为字符串”
采纳答案by SamTebbs33
Adding two char
s together actually adds their unicode values and returns an integer. If you wish to instead concatenate them together as a String
, then do the following:
将两个char
s 加在一起实际上将它们的 unicode 值相加并返回一个整数。如果您希望将它们作为 a 连接在一起String
,请执行以下操作:
return rank2 + "" + suit;
This will form a String
and append suit
to the end of rank2
. So if rank2
was 'J' and suit
was 'H', "JH" would be returned.
这将形成 aString
并附suit
加到rank2
. 因此,如果rank2
是 'J' 和suit
'H',则将返回“JH”。
回答by Zamrony P. Juhara
In Java, char
type is treated as int
. char
type supports arithmetic operation, so they can be added. So when you use + operator, it adds their values. But because char
type and int
type are different primitive types, they are incompatible for arithmetic operation.
在 Java 中,char
类型被视为int
. char
类型支持算术运算,因此可以将它们相加。因此,当您使用 + 运算符时,它会添加它们的值。但是因为char
type 和int
type 是不同的原始类型,所以它们对于算术运算是不兼容的。
If you need string concatenation then you need to change your code to:
如果需要字符串连接,则需要将代码更改为:
return String.valueOf(rank2) + String.valueOf(suit);
回答by Ophitect
the thing is that char can be read as an int value. When trying to perform arithmetic functions on character, it will be using the byte value of the character. Java cannot implicitly cast a int into a String since String is not regarded as a primitive data type in Java. You need to use .valueOf() method of the String class in order to explicitly tell Java that you want it as String.
问题是 char 可以被读取为 int 值。当尝试对字符执行算术函数时,它将使用字符的字节值。Java 不能将 int 隐式转换为 String,因为 String 在 Java 中不被视为原始数据类型。您需要使用 String 类的 .valueOf() 方法来明确告诉 Java 您希望它是 String。
String.valueOf(input_your_value_here);
回答by Avinash
The most efficient way for this would be following
最有效的方法是遵循
return new StringBuilder(rank2).append(suit).toString();
Here only one object is created and solves the purpose.
这里只创建了一个对象并解决了目的。