Home > Net >  Type mismatch: inferred type is String but Regex was expected
Type mismatch: inferred type is String but Regex was expected

Time:09-28

So, I've converted a code in Android studio from Java to Kotlin and now I'm getting these errors.

Type mismatch: inferred type is String but Regex was expected Task :app:compileDebugKotlin in app Finished


class MainActivity : AppCompatActivity() {
    lateinit var tvUsername: EditText
    lateinit var tvPassword: EditText
    lateinit var btnRegister: Button
    lateinit var img1: ImageView
   lateinit var img2: ImageView
   lateinit var img3: ImageView
   lateinit var img4: ImageView
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvUsername = findViewById(R.id.et_userName)
        this.tvPassword = findViewById(R.id.et_password)
        this.btnRegister = findViewById(R.id.btnRegister)
        img1 = findViewById(R.id.img1)
        img2 = findViewById(R.id.img2)
        img3 = findViewById(R.id.img3)
        img4 = findViewById(R.id.img4)
        tvPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (hasLength(s)) {
                    img1.setImageResource(R.drawable.checked)
                } else {
                    img1.setImageResource(R.drawable.unchecked)
                }
                if (hasSymbol(s)) {
                    img4.setImageResource(R.drawable.checked)
                } else {
                    img4.setImageResource(R.drawable.unchecked)
                }
                if (hasDigit(s)) {
                    img3.setImageResource(R.drawable.checked)
                } else {
                    img3.setImageResource(R.drawable.unchecked)
                }
                if (hasUppercase(s)) {
                    img2.setImageResource(R.drawable.checked)
                } else {
                    img2.setImageResource(R.drawable.unchecked)
                }
                if (hasLength(s) && hasUppercase(s) && hasDigit(s) && hasSymbol(s)) {
                    btnRegister.setVisibility(View.VISIBLE)
                } else {
                    btnRegister.setVisibility(View.GONE)
                }
            }

            override fun afterTextChanged(s: Editable) {}
        })
    }

    fun hasLength(value: CharSequence): Boolean {
        return value.toString().length >= 8
    }

    fun hasDigit(value: CharSequence): Boolean {
        return value.toString() matches("(.*\\d.*)")
    }

    fun hasUppercase(value: CharSequence): Boolean {
        val s = value.toString()
        return s != s.isLowerCase()
    }

    fun hasSymbol(value: CharSequence): Boolean {
        val s = value.toString()
        return !s.matches("[A-Za-z0-9 ]*")
    }
}

above is the complete code. I am getting problem in the hasUppercase() and hasSymbol function. Please help.

the error is :Type mismatch: inferred type is String but Regex was expected Task :app:compileDebugKotlin in app Finished

CodePudding user response:

Docs

String.matches expects a Regex object as a parameter. Use the String.toRegex method to create one:

s.matches("[A-Za-z0-9 ]*".toRegex())

Edit, for example:

fun hasSymbol(value: String): Boolean {
    return !value.matches("[A-Za-z0-9 ]*".toRegex())
}

Edit again

You weren't using the toRegex function for the patterns and s != s.isLowerCase() just doesn't really make sense. The following should work.

fun hasLength(value: CharSequence): Boolean {
    return value.length >= 8
}

fun hasDigit(value: CharSequence): Boolean {
    return value matches "(.*\\d.*)".toRegex()
}

fun hasUppercase(value: CharSequence): Boolean {
    return value matches "[A-Z]*".toRegex()
}

fun hasSymbol(value: CharSequence): Boolean {
    return !(value matches "[A-Za-z0-9 ]*".toRegex())
}
  • Related