Home > OS >  Kotlin - how can I print limited length
Kotlin - how can I print limited length

Time:09-11

I tried to make password generator ( only string ) using Kotlin for-loop when I run the code it's shows me outputs like this

OHDPETGDIKPCIQPHBHKWSQKXB
PJQBCSNRWDSHJJXFISDMBVAGT
XSEVXCONRMXQGHXDADQFNLJYK

its too long password so I tried to use some Kotlin functions (Size,Length) and didn't work with me maybe I just don't know the right way to do it , example I just want Password length size is from 5 to 15 chars please forget the numbers variable and x variable

Kotlin Code:

class passwordMaker {


    private val password = ('a'..'Z')   ('A'..'Z')
    private val numbers = arrayOf(1,3,4,5,2,0,4,6,7,'@','#','_','/')




    fun passwordMaker(){

        var x = numbers
        var xy = password.subList(0,25)



        for (i in xy){



            print(xy.random())
            xy.size-3
            print((x.random()))
            x.size-4

        }



    }
}

CodePudding user response:

class passwordMaker {

    fun getRandomPassword(a: Int): String {
        val characterSet =( 'a'..'Z')   ('A'..'Z')
        var random = Random(System.nanoTime())
        var password = StringBuilder()


        for (i in 0 until a){


            val psMaker = random.nextInt(characterSet.size)
            password.append(characterSet[psMaker])
        }

        return password.toString()
    }


}
  • Related