dimanche 4 octobre 2020

Password generator displays kotlin.Unit

I'm trying to make random password generator based on user input and everything is fine until i use .toCharArray().shuffle() function, but without shuffling it's too predictable beacuse it puts letters in pre-determined positions. Is there any way this code would work? Any workaround? I already tried stringbuilder but it bypasses user input so I don't know what to do now.

val chars= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~@#$%^&*()!"

 override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        if (fromUser)
        {
            when(seekBar)
            {
                sbNumberOfLetters ->
                {
                    tvLetterCount.text = progress.toString()
                    smallLetters = progress
                }

                sbNumberOfCapitalLetters ->
                {
                    tvCapitalsCount.text = progress.toString()
                    capitalLetterNumber = progress
                }

                sbNumberOfNumerals ->
                {
                    tvNumeralsCount.text = progress.toString()
                    numeralsNumber = progress
                }

                sbNumberOfSpecialChars ->
                {
                    tvSpecialCharsCount.text = progress.toString()
                    specialCharNumber = progress
                }
            }
        }
    }



 private fun generatePassword() {

        for (y in 1..numeralsNumber)
        {
            var randomLetter = Random.nextInt(0, 9)
            listOfLetters.add(chars[randomLetter].toString())
        }

        for (w in 1..smallLetters)
        {
            var randomLetter = Random.nextInt(10, 36)
            listOfLetters.add(chars[randomLetter].toString())
        }

        for (x in 1..capitalLetterNumber)
        {
            var randomLetter = Random.nextInt(36, 62)
            listOfLetters.add(chars[randomLetter].toString())
        }

        for (z in 1..specialCharNumber)
        {
            var randomLetter = Random.nextInt(63, 73)
            listOfLetters.add(chars[randomLetter].toString())
        }

        password = (listOfLetters.joinToString(separator = "",)).toCharArray().shuffle().toString()

        tvGeneratedPassword.text = password

        listOfLetters.clear()
    }



Aucun commentaire:

Enregistrer un commentaire