0

I have a "for-loop" in Kotlin which is going to run my code 6 times.

I also have a textView on the app and want to see these 6 results shown there.

I can easily println() the results.

However, If I set the text of textView to these results, it only gets the last result.

What I like to do printing out all 5 results in textView (suggestedNums ) as each result is a separate line.

Is it even possible? Any help appreciated.

Thanks.

for (i in 1..6) {
   val s: MutableSet<Int> = mutableSetOf()

   //create 5 numbers from numbers
   while (s.size < 5) {
      val rnd = (numbers).random()
      s.add(rnd)
   }

   // remove all 5 random numbers from numbers list.
   numbers.removeAll(s)

   // sort 5 random numbers and println
   println(s.sorted())

   // set suggestedNums text to "s"
   suggestedNums.text = s.sorted().toString()
}

1 Answer 1

2

You can do it in 2 ways

  1. replace

    suggestedNums.text = s.sorted().toString()

    with

    suggestedNums.text = suggestedNums.text.toString() + "\n" + s.sorted().toString()

  2. Create a string and append the results with "\n" and set the text outside the for loop

Sign up to request clarification or add additional context in comments.

1 Comment

suggestedNums.append(s.sorted().toString()) suggestedNums.append("\n") this sorted out, thanks for the help @Viswanath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.