简体   繁体   中英

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:

  • get text from JTextArea
  • Go through every char in JTextArea and adds it to a string called currentWord until it finds a line break char //(does such a thing exist?)
  • When the loop detects a line break, it adds currentWord to the arrayList and sets currentWord to empty.
  • After the loop ends, add currrentWord to the ArrayList.

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

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);

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 String s to the list.

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

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"));
String line[]=jTextArea1.getText().split("\\n");
List<String> list=Arrays.asList(line);

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.
}
final List<String> lines = Arrays.asList(textArea.split("\n"));
scanner s = new scanner(JTextArea.getText());
while (s.hasNext()) {
   arrayList.add(s.nextLine())
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM