简体   繁体   中英

How to take multi line input using jtextarea in java netbeans?

What i want to do is take input from the user in multi lines, suppose user inputs some details in multi line textarea control
================
Sarah
Jones
Chris
Samantha
================
Now i want to insert these lines into an array, modify the details a bit
then show them in a second textarea or label.
I want the output something like this
================
Welcome sarah
welcome jones
welcome chris
welcome samantha
================

I heard we can do this using split method but it is not giving me the result
i want. This is the code i prepared so far.


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
       String[] names = jTextArea1.getText().split("\\.");
       for(int i=0;i<names.length;i++)
       {
        jTextArea2.setText("welcome "+names[i]);
       }
}

output is
==============
welcome sarah
jones
chris
samantha
==============
welcome is only printed once, what am i doing wrong?

private void jButton1ActionPerformed (ActionEvent evt){
   // are you sure that this split returns the names? Maybe you should split by \\n
   String[] names = jTextArea1.getText().split("\\n");
   // build the text to set into textarea2
   String text = "";
   for(int i=0;i<names.length;i++)
   {
      text += "welcome "+names[i]+"\n";
   }

   jTextArea2.setText(text);
}

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