Search code examples
javanetbeansinputmultiline

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?


Solution

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