Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
kotlin:将字符串与Int值串联_Kotlin_Concatenation - Fatal编程技术网

kotlin:将字符串与Int值串联

kotlin:将字符串与Int值串联,kotlin,concatenation,Kotlin,Concatenation,我有一个模型数据类。此类的值为Int(val amount:Int) 当我要初始化此值并将其与字符串连接时,请给出以下错误: java.lang.NumberFormatException: For input string: "1 : amount" 我使用此代码将字符串与Int值连接起来 var number = 0 "$number : amount".toInt() 所以我的问题是如何将字符串与Int值连接起来 谢天谢地,这是先进的 当我想初始

我有一个模型数据类。此类的值为Int(val amount:Int)

当我要初始化此值并将其与字符串连接时,请给出以下错误:

java.lang.NumberFormatException: For input string: "1 : amount"
我使用此代码将字符串与Int值连接起来

var number = 0
"$number : amount".toInt()
所以我的问题是如何将字符串与Int值连接起来

谢天谢地,这是先进的

当我想初始化这个值并用字符串连接它时

你的意思是
format
而不是
toInt
我想:

"%d : amount".format(amount)
或使用可变保持架:

val amount = 1
val output:String = "$amount : amount"

根据

fun String.toInt():Int

将字符串解析为整数并返回结果

异常NumberFormatException-如果字符串不是有效的 数字的表示。


您的字符串
“1:amount”
不是正确的数字,因为您得到的
NumberFormatException

无法正常工作。我发现类型不匹配。“%d:amount”。格式(数字)以及此。。。val amount=1 val输出:String=“$amount:amount”我只需要用我的Int值(number)添加一些文本。像这样的事情:数量:0@BahadorEslami如果我理解,您有一个Int,并且希望返回一个类似于
amount:0
的Int?如果是这种情况,那么它是不可能的,您不能将字符串作为IntSo返回,不可能执行此操作///var num:Int=1 var numWithText:Int=“$num:amount”对吗?