I am following the numerous guides from this website: https://www.w3schools.com/kotlin/kotlin_arrays.php I am experimenting along the way with my own ideas and such.
For the section detailing how to access elements in an array, I decided that I want to create an output that uses correct grammar in English. I want the list to be displayed as such:
"Volvo, BMW, Ford and Audi."
How can I achieve this? I`m of course an absolute beginner.
Thank you!
fun main()
{
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println(cars[3]) // Prints "Mazda"
if ("Mazda" in cars)
{
println("It exists!")
}
else
{
println("It doesn`t exist!")
}
cars[3] = "Audi"
println(cars[3])
if ("Mazda" in cars)
{
println("It exists!")
}
else
{
println("It doesn`t exist!")
}
println("There are " cars.size " elements in this array:")
for (w in cars) // The dilemma begins from here
while (w < cars.size - 1)
{
print(w ", ")
}
else
{
print(w "and " cars[3] ".")
}
}
CodePudding user response:
@Tenfour04
Thank you for your post. Here is what I settled at before your last post
for ((i, w) in cars.withIndex())
{
if (i < cars.size - 1)
{
print(w ", ")
}
else
{
for (i in 0..cars.size-4)
print("and " cars[3])
}
}
which gave me the output of
Volvo, BMW, Ford, and Audi
After I`ve corrected it with your solution, thank you very much.
The output is still the same:
Volvo, BMW, Ford, and Audi
Now I am wondering how I can remove the third comma. The one after "Ford", so that the final output would be:
Volvo, BMW, Ford and Audi
CodePudding user response:
while
is a loop with a condition (in the parentheses after it). That's not what you want. And in your comment, you replaced for
with println
, which doesn't make sense.
And your else
branch makes the mistake of using both w
and cars[3]
in you final output, two different ways of getting the name of the last car, so it appears twice in your output.
Since you are using both the elements of the array and their indices, you should iterate both at once using withIndex
, like this.
Here's how you can iterate both the indices and the cars so you can do an if
condition on the index (position) in the list of each item.
for ((i, w) in cars.withIndex()) {
if (i < cars.size - 1)
{
print(w ", ")
}
else
{
print("and " w ".")
}
}
But it's cleaner to use string templates like:
for ((i, w) in cars.withIndex()) {
if (i < cars.size - 1)
{
print("$w, ")
}
else
{
print("and $w.")
}
}
When you have short single function calls in each branch of if/else, it sometimes is cleaner looking code to use when
instead:
for ((i, w) in cars.withIndex()) {
when {
i < cars.size - 1 -> print("$w, ")
else -> print("and $w.")
}
}
Or for a completely different strategy, you could iterate all but the last item by dropping it, and then use just the last item:
for (car in cars.dropLast()) {
println("$car, ")
}
print("and ${cars.last()}")
For your comment in your own answer, you can use a when
statement with three conditions:
for ((i, w) in cars.withIndex()) {
when {
i < cars.size - 2 -> print("$w, ")
i == cars.size - 2 -> print("$w ")
else -> print("and $w.")
}
}