Mike
Mike

Reputation: 331

Lines of JTextArea to an ArrayList<String>

How would one convert every line in a JTextArea into an ArrayList<String>? That is, how does one detect line breaks in a JTextArea?

Here is my current pseudo code implementation:

1. Is there an easier way to do this?
2. Does this implementation scale well with size? (I'm guessing no)

Upvotes: 3

Views: 13536

Answers (7)

guest
guest

Reputation: 1

scanner s = new scanner(JTextArea.getText());
while (s.hasNext()) {
   arrayList.add(s.nextLine())
}

Upvotes: 0

SerefAltindal
SerefAltindal

Reputation: 349

String line[]=jTextArea1.getText().split("\\n");
List<String> list=Arrays.asList(line);

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201537

Or, on reflection, I might use your original algorithm like this

private static List<String> toList(JTextArea textArea) {
  List<String> al = new ArrayList<String>(); // return list.
  if (textArea != null) {
    String val = textArea.getText(); // the text.
    StringBuilder sb = new StringBuilder(); // for the current word.
    for (char c : val.toCharArray()) {
      if (Character.isWhitespace(c)) { // ANY white space.
        if (sb.length() > 0) { // is there a current word?
          al.add(sb.toString()); // add it to the list.
          sb.setLength(0); // clear the word.
        }
      } else {
        sb.append(c); // NOT white space, add it to current word.
      }
    }
  }
  return al; // return the list.
}

Upvotes: 0

Luke
Luke

Reputation: 1306

JTextArea txArea = new JTextArea();
txArea.setText("line1\nline2\nline3");
String txt = txArea.getText();
String [] arrayOfLines = txt.split("\n");
ArrayList<String> linesAsAL = new ArrayList<String>();
for(String line: arrayOfLines){
    linesAsAL.add(line);
}

Or instead of adding lines to ArrayList on a loop something more elegant:

List<String> lines = Arrays.asList(txt.split("\n"));

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

Start by using JTextArea#getText to get ALL the text from the text area.

Once you have that, you can use String#split, passing "\n" as the value. This will return an array of String's split on the new line.

If you want to extract each word from the line, you could split it again, presumably on " ", which will give you each word.

You can then use Arrays.asList to convert this to a List (of words or lines) and (assuming you've already created an instance of one), use ArrayList#addAll to add all the Strings to the list.

If you're only interested in each unquie word, you could use a Set to filter out duplicate words

Upvotes: 2

Sage
Sage

Reputation: 15438

embrace the power of Strig.split(regex) and Arrays.asList function:

    String s[] = jTextArea1.getText().split("\\r?\\n");
    ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
    System.out.println(arrList);

Upvotes: 10

chackley
chackley

Reputation: 71

final List<String> lines = Arrays.asList(textArea.split("\n"));

Upvotes: 0

Related Questions