2
\$\begingroup\$

I wrote some code which divides a line through the words of the text so that each substring is no longer than MaxWidth. It works well, but it's very slow.

Pattern pattern = Pattern.compile("(.{1," + symbols + "}(\\b|\\s))"); // symbols - MaxWidth
Pattern pattern2 = Pattern.compile("\\s.*");
while ((line = in.readLine()) != null) { // reading file by lines 
 String dopLine = "";
 if (pattern2.matcher(line).matches()) {
 // If a line begins with a space, it is the beginning of a paragraph and need to add \ n
 if(!tempDopLine.equals("")) { // tempDopLine - Some part of the previous line, which is not full screen
 tempStringBuffer.append(tempDopLine);
 tempStringBuffer.append("\n");
 lineCounter++;
 if (lineCounter == lines) {
 addPage(tempStringBuffer); // create page
 tempStringBuffer = new StringBuilder("");
 lineCounter = 0;
 numberOfPages++;
 }
 }
 tempDopLine = "";
 dopLine = line;
 } else { 
 dopLine = tempDopLine + " " + line; // if this line
 }
 Matcher matcher = pattern.matcher(dopLine); // divide a string into a substrings
 HashMap<Integer, String> temp = new HashMap<Integer, String>();
 int i = 0;
 while (matcher.find()) {
 temp.put(i, matcher.group());
 i++;
 }
 for(i = 0; i < temp.size(); i++) {
 if (i<(temp.size()-1)) {
 String tempL = temp.get(i);
 tempStringBuffer.append(tempL);
 tempStringBuffer.append("\n");
 lineCounter++;
 if (lineCounter == lines) {
 addPage(tempStringBuffer);
 tempStringBuffer = new StringBuilder("");
 lineCounter = 0;
 numberOfPages++;
 }
 } else {
 tempDopLine = temp.get(i); // The last part of the string remember to display it along with the next line
 }
 } 
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 13, 2012 at 15:52
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Few thought,

You do a while loop ( while( matcher.find() ) to find all matchs, then you do a for loop to deal with it. I think that can be done in only one loop.

in you last for loop, you could remove the first if :

 for(i = 0; i < (temp.size() -1); i++) {
 String tempL = temp.get(i);
 tempStringBuffer.append(tempL);
 tempStringBuffer.append("\n");
 lineCounter++;
 if (lineCounter == lines) {
 addPage(tempStringBuffer);
 tempStringBuffer = new StringBuilder("");
 lineCounter = 0;
 numberOfPages++;
 }
 } // End for loop
 tempDopLine = temp.get(temp.size()-1); 
} 

Other suggestion the HashMap temp got be only a array, because right now your code do a lot of autoboxing ( from int to Integer) add and to retrieve information from your HashMap.

Edit: Here a quick example to merge your two loops:

String tempL = matcher.group();
while (matcher.find()) {
 tempStringBuffer.append(tempL);
 tempStringBuffer.append("\n");
 lineCounter++;
 if (lineCounter == lines) {
 addPage(tempStringBuffer);
 tempStringBuffer = new StringBuilder("");
 lineCounter = 0;
 numberOfPages++;
 }
 tempL = matcher.group();
} 
tempDopLine = tempL; 
answered Feb 13, 2012 at 16:11
\$\endgroup\$
2
  • \$\begingroup\$ To combine the two loop I need to get the number of elements found in the Matcher. How to do it? In my code - last match must writing to tempDopLine \$\endgroup\$ Commented Feb 13, 2012 at 18:36
  • \$\begingroup\$ Thank you very mych. I using this, but it did not give a large increase in performance. It seems to me that such a low rate because of the regular expressions. They can be something like speed? \$\endgroup\$ Commented Feb 15, 2012 at 7:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.