I have the following code which will call function save() based on the first five characters of the string in the EditText. What I am trying to save to the database is anything after the first five characters in the edit text, if the condition is met.
Below code works fine when the string in the edit text is more than five characters. If the length of the string is less than 5 then the app crashes. In order to avoid this, I added the second block of code to find the length of the string but it does not work. Can you check where is the issue?
val pass =findViewById<EditText>(R.id.editText).text.toString()
var password: String = pass.substring(0,5)
if (password == "hello"){
save()
Toast.makeText(this, "Database updated", Toast.LENGTH_LONG).show()
}
else
{
Toast.makeText(this, "Unauthorized user", Toast.LENGTH_LONG).show()
}
}
I tried the following code in the REPL and it returned, following lines or error.
java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.substring(String.java:1963)
var pass="gl"
var password = pass.substring(0,3)
val len = password.length
println(len)
print(password)
When the length of the variable pass is equal to or more than 3 characters then it executes without error in REPL.
I am on Android Studio using Kotlin.
NB: The Second block of code, as is, not what is written into the editor. It's is the sample what I was trying in the REPL.