1

I have automated some flow of filling in a form from a website by taking the fields data from a csv.

Now, for the address there are 3 fields in the form:

Address 1 ____________

Address 2 ____________

Address 3 ____________

Each field have a limit of 35 characters, so whenever I get to 35 characters im continuing the address string in the second address field...

Now, the issue is that my current solution will split it but it will cut the word if it got to 35 chars, for instant, if the word 'barcelona' in the str and 'o' is the 35th char so in the address 2 will be 'na'.

in that case I want to identify if the 35th char is a middle of a word and take the whole word to the next field.

this is my current solution:

private def enterAddress(purchaseInfo: PurchaseInfo) = {

    val webElements = driver.findElements(By.className("address")).toList
    val strings = purchaseInfo.supplierAddress.grouped(35).toList
    strings.zip(webElements).foreach{
      case (text, webElement) => webElement.sendKeys(text)
    }
  }

I would appreciate some help here, preferably with Scala but java will be fine as well :)

thanks allot!

nick shmick
  • 905
  • 1
  • 9
  • 25

2 Answers2

2

Since you said you'd accept Java code as well... the following code will wrap a given input string to several lines of a given maximum length:

import java.util.ArrayList;
import java.util.List;

public class WordWrap {

  public static void main(String[] args) {
    String input = "This is a rather long address, somewhere in a small street in Barcelona";
    List<String> wrappedLines = wrap(input, 35);
    for (String line : wrappedLines) {
      System.out.println(line);
    }
  }

  private static List<String> wrap(String input, int maxLength) {
    String[] words = input.split(" ");
    List<String> lines = new ArrayList<String>();

    StringBuilder sb = new StringBuilder();
    for (String word : words) {
      if (sb.length() == 0) {
        // Note: Will not work if a *single* word already exceeds maxLength
        sb.append(word);
      } else if (sb.length() + word.length() < maxLength) {
        // Use < maxLength as we add +1 space.
        sb.append(" " + word);
      } else {
        // Line is full
        lines.add(sb.toString());
        // Restart
        sb = new StringBuilder(word);
      }
    }
    // Add the last line
    if (sb.length() > 0) {
      lines.add(sb.toString());
    }

    return lines;
  }
}

Output:

This is a rather long address,
somewhere in a small street in
Barcelona

This is not necessarily the best approach, but I guess you'll have to adapt it to Scala anyway.

If you prefer a library solution (because... why re-invent the wheel?) you can also have a look at WordUtils.wrap() from Apache Commons.

Marvin
  • 13,325
  • 3
  • 51
  • 57
0

Words in the English language are delimited by space (or other punctuation, but that is irrelevant in this case unless you actually want to wrap lines based on that), and there are a couple of options for using this to your advantage:

One thing you could potentially do is take a substring of 35 characters from your string, use String.lastIndexOf to figure out where the space is, and add only up to that space to your address line, then repeating the process starting from that space character until you have entered the string.

Another method (showcased in Marvin's answer) is to just use String.split on spaces and concatenate them back together until the next word would cause the string to exceed 35 characters.

Brandon McKenzie
  • 1,655
  • 11
  • 26