I have a method that checks if a quote generated from an API is within 130 characters, before split it into two parts, the front part is the quote and second part is the author.
The html output of the API quote is: (for example) "Kindness in words creates confidence. (Lao Tzu)"
I want to show them in two separate textviews and remove the brackets. Textview 1 - Quote Textview 2 - Author(removed brackets)
Unfortunately, the app crashed and show IndexOutOfBoundsException sometimes, is there any other method to do this?
Thank you in advanced.
private void check(String quote){
//check length
int length = quote.length();
if (length <= 130) {
//split to quote and author strings
String[] parts = quote.split(Pattern.quote("(")); // Split on ( into 2 parts, quote and author
String part1 = parts[0]; //quote
String part2 = parts[1]; //author with )
String removedSymbolsPart2 = part2.replaceAll("\\)", ""); //remove ) symbol
String finalQuote = Html.fromHtml(part1).toString();
String finalAuthor = Html.fromHtml(removedSymbolsPart2).toString();
quoteTextViewMain.setText(finalQuote);
authorTextViewMain.setText(finalAuthor);
} else {
//get another quote
getQuote();
}
}
Index out of bound error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amethyst.secretgarden, PID: 21802
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.amethyst.secretgarden.MainActivity.check(MainActivity.java:83)
at com.amethyst.secretgarden.MainActivity.access$000(MainActivity.java:24)
at com.amethyst.secretgarden.MainActivity$4.success(MainActivity.java:106)
at com.amethyst.secretgarden.MainActivity$4.success(MainActivity.java:103)
at com.github.kittinunf.fuel.core.DeserializableKt$response$7.invoke(Deserializable.kt:150)
at com.github.kittinunf.fuel.core.DeserializableKt$response$7.invoke(Unknown Source:4)
at com.github.kittinunf.fuel.core.DeserializableKt$response$asyncRequest$1$1.invoke(Deserializable.kt:192)
at com.github.kittinunf.fuel.core.DeserializableKt$response$asyncRequest$1$1.invoke(Unknown Source:0)
at com.github.kittinunf.fuel.core.RequestExecutionOptionsKt$sam$java_lang_Runnable$0.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)
I/Process: Sending signal. PID: 21802 SIG: 9
I would use
String [] parts = quote.split ("("); // no pattern neeeded
if (parts.length > 1) {
String part1 = parts[0]; //quote
String part2 = parts[1]; //author with )
String removedSymbolsPart2 = part2.replace(")", ""); // not replaceAll
}