fun main(args: Array<String>) {
//receive names from user
print("Enter the names: ")
val names = readLine()?.split("/n")
//receive number of groups from the user
print("Enter number of groups: ")
val group = readLine()
print(names.chunked(group))
}
I was trying to print the users into evenly separated groups. I found that I could use chunked to accomplish this. But when I try to input the amount of groups to the chunked function nothing happens. it outputs "kotlin.Unit" I'm guessing this is an error code. But I don't know how I messed up my code.
As mentioned in the comments, this code does not compile. It still has the following problems:
namescan benull, so you'll either have to ensure that it isn't, which you could do using the Elvis operator:Or you can use a safe call on
nameswherever you use it:If you look at the documentation for chunked, you'll see that it has one parameter,
sizeof typeInt. However, you are passing aStringto it, which you've read from the user. You'll have to try to convert this string to an int before passing it into chunked:Note that I'm trimming and splitting the user input to increase chances of retrieving a valid int.
Fixing these problems makes your code compile. However, there's one more thing to fix. Assuming you meant the newline character with
/n, you are now using the newline character as a separator for the names as well as to indicate that the user is done entering names. I suggest you use something different, like a comma. In total, this could give something like the following.