Android : Getting OutOfBoundsException while pagination a String

on Sunday, September 7, 2014


I am creating an epub reader for android. For the pagination part, I am trying to get the whole string content and then search for space in the string. Then I get the text height and compare it with the screen height. if still (text height < screen height) I loop through the string and do the same thing in a while loop.


Every thing went well, but when it comes to the end of the string I get IndexOutOfBoundsException. I have attached the screenshot of the Logcat below.


enter image description here


The code I used to get the no of pages is like this



public String getNoOfPages(String text){


String remainingString = "";

try{
int screenHeight = getScreenHeight();
String originalText = text;
String strToModify = text;


StringBuilder newString = new StringBuilder();
StringBuilder oldString = new StringBuilder();


int startIndex = 0;
String strToFind = " ";
int index = strToModify.indexOf(strToFind,startIndex);

newString.append(originalText.substring(startIndex, index+1));
oldString.append(newString.toString());
startIndex = index+1;

int textHeight = getTextHeight(newString.toString());

while(textHeight < screenHeight){
index = strToModify.indexOf(strToFind,startIndex);
oldString.replace(0,oldString.toString().length(),newString.toString());
newString.append(originalText.substring(startIndex, index+1));
startIndex = index+1;
textHeight = getTextHeight(newString.toString());
}

remainingString = originalText.substring(oldString.length()-1,originalText.length());

}catch(Exception e){
Log.d("chathura123","Error in getNoOfPages " );
e.printStackTrace();

}

return remainingString;
}


The logic is when the remaining string is an empty string("") ,it means that is the end of the content of the page. So I want to check until it returns an empty string.


What is the wrong with this? Why I am getting OutOfBoundsException?


Thank you.


0 comments:

Post a Comment