views:

371

answers:

2

I have a text area wherein i have limited the user from entering more that 15 characters in one line as I want to get the free flow text separated into substrings of max limit 15 characters and assign each line an order number. This is what I was doing in my java class:

int interval = 15;                
items = new ArrayList();
 TextItem item = null;
 for (int i = 0; i < text.length(); i = i + interval) {
  item = new TextItem ();
  item.setOrder(i);

  if (i + interval < text.length()) {
    item.setSubText(text.substring(i, i + interval));
    items.add(item);
  } 
  else {
    item.setSubText(text.substring(i));
    items.add(item);
  }
}

Now it works properly unless the user presses the enter key. Whenever the user presses the enter key I want to make that line as a new item having only that part as the subText.

I can check whether my text.substring(i, i + interval) contains any "\n" and split till there but the problem is to get the remaining characters after "\n" till next 15 or till next "\n" and set proper order and subText.

A: 

You could reverse the order of doing things: First, split the content of your text area at occurences of '\n'. Then take each of the resulting strings and split them into parts of maximum length 15.

Edit: I'm not totally sure about what you want to do, but here's what I would do. Note that I'm not a Java programmer, so I might be making false assumptions, and the following is untested.

int interval = 15;                
items = new ArrayList();
lines = text.split("\n");
for (int i = 0; i < lines.length(); i++)
{
  str = lines[i];
  while (str.length() > interval)
  {
    items.add(str.substring(0,interval)); // add the first 15 characters of str to the list
    str = str.substring(interval); // ... and remove them from str
  }
  items.add(str); // add the rest
}

After that, items is an ArrayList of strings that are in the correct order. It should be possible to extend this code to whatever additional information you need to store with the actual strings.

balpha
Thanks balpha.But as i mentioned the problem i have is to set proper oder number and subText...how to loop around this...any help.
desmiserables
I have edited my answer.
balpha
Thanks Balpha!Only thing is that if we split the string with '/n' then we are losing out the lines which only contains the '/n'.The idea is that if the user presses only the enter key also still those lines have to be accounted unless such occurrences are at the beginning of the text area without any characters entered before the user presses enter key.
desmiserables
No, you won't miss the empty lines. According to the Java String.split() documentation at http://www.j2ee.me/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String) the split will retain empty strings in the middle. And discarding the empty ones at the beginning shouldn't be to hard.
balpha
Thanks Balpha...I am facing an issue with this code...basically,items.add(str.substring(0,interval)); is getting overridden byitems.add(str); it is getting added to the same item....after the while loop
desmiserables
Actually for each interval i need to add a new item as well...
desmiserables
I'm really sorry, but I don't understand what you're saying.
balpha
A: 

Is this what you're trying to do?

public static void main(String[] args)
{
  String str =
  "\n\n123456789ABCDEF123456\n123456789AB\n\n123" +
  "456789ABCDEF123456789AB\n123456789ABCDEF\n";

  List<String> parts = new ArrayList<String>();

  // remove leading line separators
  str = str.replaceFirst("\\A[\r\n]+", "");

  // match up to 15 non-newline characters, but don't
  // match a zero-length string at the end
  Matcher m = Pattern.compile("(?!\\z).{0,15}").matcher(str);
  while (m.find())
  {
    parts.add(m.group());
  }

  for (String s : parts)
  {
    System.out.println(s);
  }
}

result:

123456789ABCDEF
123456

123456789AB


123456789ABCDEF
123456789AB

123456789ABCDEF
Alan Moore