4

A very basic question, What is the right approach to concatenate String to an Int?

I'm new in Kotlin and want to print an Integer value preceding with String and getting the following error message.

for (i in 15 downTo 10){ print(i + " "); //error: None of the following function can be called with the argument supplied: print(i); //It's Working but I need some space after the integer value. } 

Expected Outcome
15 14 13 12 11 10

2
  • 5
    Use this syntax print("$i ") when using Kotlin. Commented May 31, 2019 at 6:57
  • Thank you @JeelVankhede, it's working for me ;) Commented May 31, 2019 at 7:42

7 Answers 7

9

You've got several options:

1. String templates. I think it is the best one. It works absolutely like 2-st solution, but looks better and allow to add some needed characters.

print("$i") 

and if you want to add something

print("$i ") print("$i - is good") 

to add some expression place it in brackets

print("${i + 1} - is better") 

2. toString method which can be used for any object in kotlin.

print(i.toString()) 

3. Java-like solution with concatenating

print("" + i) 
Sign up to request clarification or add additional context in comments.

2 Comments

In Java print( "" + i) and print( i + "") both work but in Kotlin only print( "" + i ) works. Why print( i + "") does not work in Kotlin?
In kotlin concatenation operation isn't defined in this way. From some point of view it even seems logical - you can add any number to string, but what you should expect from adding string to number? Anyway, kotlin haven't such option)
3

$ dollar – Dollar symbol is used in String templates that we’ll be seeing next

for (i in 15 downTo 10){ print("$i ") } 

Output : 15 14 13 12 11 10

1 Comment

What the point of str val? You are also inconsistent about using semicolon.
2

You can use kotlin string template for that:

for (i in 15 downTo 10){ print("$i "); } 

https://kotlinlang.org/docs/reference/basic-types.html#string-templates

Comments

1

The Int::toString method does what you're looking for. Instead of explicit loops, consider functional approaches like map:

(15 downTo 10).map(Int::toString).joinToString { " " } 

Note that the map part is even redundant since joinToString can handle the conversion internally.

Comments

1

The error you get is because the + you're using is the integer one (it is decided by the left operand). The integer + expects 2 integers. In order to actually use the + of String for concatenation, you would need the string on the left, like "" + i + " ".

That being said, it is more idiomatic in Kotlin to print formatted strings using string templates: "$i "

However, if all you want is to print integers with spaces in between, you can use the stdlib function joinToString():

val output = (15 downTo 10).joinToString(" ") print(output) // or println() if you want to start a new line after your integers 

Comments

0

Just cast to String:

for (i in 15 downTo 10){ print(i.toString() + " "); } 

Comments

0

You should use the $ . You can also use the + but it could get confusing in your case because the + has is also an operator which invokes the plus() method which is used to sum Integers.

for (i in 15 downTo 10){ print("$i "); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.