Parse JTextArea in Java

Posted on 2004-11-26
Last Modified: 2007-11-27
User write something into JTextArea .
I have an array with specified words for example {"name1","name2","name3"}
If i found this words in textarea after the user writed i must put into array all word from line wher i found a word form list.
For example: textarea: name1 1,23,4;
name2 asasas;
name3 jkjkjk;
When i found the word "name1" i must put into array name1 1,23,4
I don't know how i parse all the  lines from textarea and get this information into array.

Question by:cipriancomsa
    LVL 9

    Expert Comment

    Hi cipriancomsa,

    Firstly, you want to extract the text from the JTextArea, and assign it to a String, like so:

    JTextArea jta = new JTextArea();
     . . .
    String content = jta.getText();

    Then tokenize it to an array of each line, like so:

    (Import java.util.*; for this..)

    StringTokenizer st = new StringTokenizer( content, "\n" );
    String [] lines = new String [ st.countTokens() ];

    for ( int i=0; i<st.countTokens(); i++ ) {
       lines[i] = st.nextToken();

    // Now, you have an array of each line in the text field. Then, you can use the "StringTokenizer" class again, or the split() method to tokenize, and extract other data.

    Hope that helps,
    LVL 9

    Accepted Solution

    >"When i found the word "name1" i must put into array name1 1,23,4"
    Do you need to put into the array "name1 1,23,4"? Or, do you want to put into an array representing "name1" the value "1,23,4"? I'm going to assume the first ...

    String [] names = { "name1", "name2", "name3" };

    Then, after using the code in my previous comment...

    for ( int a=0; a<lines.length; a++ ) {
       for ( int b=0; b<names.length; b++ ) {
          StringTokenizer st1 = new StringTokenizer( lines[a], names[b] );
          while ( String str = st1.nextToken() && str != null ) {
             names [b] = names[b] + str;

    That *should* work, but I haven't tested it, and kinda rushed it, but it should at least get you on the right track..


    Author Comment

    Thank you for answer.
    LVL 9

    Expert Comment

    8-) ThanQ.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now