0

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.

3 Answers 3

1

First check the length of the string and then do work according to size of length like

val length = pass.length
if(length<5){
    println(len);
}else{
   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()
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked with a slight change. Changed pass.length() to pass.length.
1

In Java/Kotlin, a String is a sequence of characters, each character has a given index, the index is starting from 0, for example:

"Hello"

length: 5 (5 characters)
startIndex: 0
endIndex: length - 1 = 5 - 1 = 4

Character    Index
    H          0
    e          1
    l          2
    l          3
    o          4

From substring Kotlin documentation:

Returns the substring of this string starting at the startIndex and ending right before the endIndex.

fun String.substring(startIndex: Int, endIndex: Int): String

Back to your example:

"gl"

length: 2
startIndex: 0
endIndex: length - 1 = 2 - 1 = 1

Character    Index
    g          0
    l          1

pass.substring(0,3) will returns a substring at the startIndex (0) and ending right before the endIndex (endIndex - 1 = 3 - 1 = 2). But 2 is an invalid index, so the program throws IndexOutOfBoundsException and make your app crash.

You can check the length of editText before calling substring() on it.

// Define this variable in scope of class
private val PASSWORD_LENGTH_MIN = 5


val pass = findViewById<EditText>(R.id.editText).text.toString()

if (pass.length < PASSWORD_LENGTH_MIN) {
    Toast.makeText(this, "Password length too short.", Toast.LENGTH_LONG).show()
} else {
    var password: String = pass.substring(0, PASSWORD_LENGTH_MIN)

    if (password == "hello") {
        save()
        Toast.makeText(this, "Database updated", Toast.LENGTH_LONG).show()
    } else {
        Toast.makeText(this, "Unauthorized user", Toast.LENGTH_LONG).show()
    }
}

Comments

0

in your save() function put the code below to check if the string"s length is more than 5 characters

val pass =findViewById<EditText>(R.id.editText).text.toString()

if (pass.length>5){
     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()
            }
}else{
                Toast.makeText(this, "Length is less than 5 Chars", Toast.LENGTH_LONG).show()

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.