• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1628
  • Last Modified:

Parse JTextArea in Java

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.

0
cipriancomsa
Asked:
cipriancomsa
  • 3
1 Solution
 
DrWarezzCommented:
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,
[r.D]
0
 
DrWarezzCommented:
>"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..

gL,
[r.D]
0
 
cipriancomsaAuthor Commented:
Thank you for answer.
0
 
DrWarezzCommented:
8-) ThanQ.
[r.D]
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now