Win a copy of Modern frontends with htmx this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Trying to teach myself kotlin, any help would be great

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Any small challenges like making a small app in kotlin would be really helpful, I have been trying to build a small program that you can pick a few random numbers, don't repeat any numbers and prints the numbers in order ( 1..150)
Is this code effective and how could I improve the code?

import kotlin.random.Random

fun main(args: Array<String>) {

/** Random number picker with no repeating numbers.
* will display them in number order.
* Random numbers upto 150.
* Upto 150 numbers balls.
*
* randomList(max number int 1..) ( how many numbers/ balls)
**/
randomList( 27, 30)
}

fun randomList(maxNumber : Int, totalNumbers : Int){
if (totalNumbers > 150){
}else if (totalNumbers > maxNumber){
print("maxNumber has to be same or higher than totalNumbers")
}else{

var list = mutableListOf<Int>()
var totalNumbersMinus1 = totalNumbers-1

var randomList = (1..maxNumber).shuffled().take(totalNumbers)
for(I in 0..totalNumbersMinus1){

var number = randomList[I]

}
list.sort()
for(I in 0..totalNumbersMinus1){
print("\${list[I]} ")
}
}

}

Master Rancher
Posts: 4764
71
• 1
• Number of slices to send:
Optional 'thank-you' note:
Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.

My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.

IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.

For the rest of the code...

You seem to be doing the same thing several different ways. The one I like best is this one:

Here you've accomplished most of what you want in one line - great!  You don't need to copy another list with the same contents.   And you don't need to worry about only getting totalNumbers elements from this list - it's already limited in length, to have only totalNumbers elements.  So you just need sort it, and print each element.  Elsewhere you use the sort() method, which is good, but won't work on randomList because it's immutable.  You could fix thaqt with toMutableList():

But it's easier to replace sort() with sorted().  The latter creates a new immutable list, in sorted order, leaving the original immutable list alone:

But at this point, the randomList variable itself is no longer needed:

Jason Edwards
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Hi Mike Simmons,
Thanks for looking over my code and giving me some help.

Mike Simmons wrote:Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.

I was paying around with inputs, testing my code and checking the errors.

Mike Simmons wrote:My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.

My workplace is quiet for the next 2 weeks. So I was using https://play.kotlinlang.org/ to build the app(spent about 3 hours on the code)

Mike Simmons wrote:IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.

Will do from now on.

Mike Simmons wrote:You seem to be doing the same thing several different ways. The one I like best is this one:

I really like this code but from what I can tell, its read only!, you can print the full list or an element, looked online for random list but could not find any post talking about .sorted() or .forEach({print("\$it ")})
so moved the numbers over to an array I could sort and edit the list.

Thank you for the help.
I really enjoy reading code like this, makes perfect sense.

Mike Simmons wrote:(1..maxNumber).shuffled().take(totalNumbers).sorted().forEach({print("\$it ")})

Jason Edwards
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:

Jason Edwards wrote:Hi Mike Simmons,
Thanks for looking over my code and giving me some help.

Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.
I was paying around with inputs, testing my code and checking the errors.

My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.
My workplace is quiet for the next 2 weeks. So I was using https://play.kotlinlang.org/ to build the app(spent about 3 hours on the code)

IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.
Will do from now on.

You seem to be doing the same thing several different ways. The one I like best is this one:

I really like this code but from what I can tell, its read only!, you can print the full list or an element, looked online for random list but could not find any post talking about .sorted() or .forEach({print("\$it ")})
so moved the numbers over to an array I could sort and edit the list.

Thank you for the help.
I really enjoy reading code like this, makes perfect sense.

 I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad? a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com