I have a JTextArea and would like to be able to add multiple arguments to like so: messageArea.setText("Hi", name, "how are you today?"); However, I am unable to do so, I do not know how to add multiple arguments to the JTextArea.
            Asked
            
        
        
            Active
            
        
            Viewed 635 times
        
    0
            
            
         
    
    
        mKorbel
        
- 109,525
- 20
- 134
- 319
 
    
    
        Harry Kitchener
        
- 255
- 1
- 2
- 8
- 
                    Do you mean concatenation of strings? Like combine those three strings so they can be passed as one string? If so, just use '+'. So it would be "hi" + name + " how are you today?" – Kyle Jun 05 '14 at 19:21
1 Answers
2
            The setText() method takes a single String argument, so you have to concatenate the strings, you want to display.
StringBuilder sb = new StringBuilder();
sb.append("Hi").append(name).append(", how are you?");
messageArea.setText(sb.toString());
Other method is to simply use the + operator:
messageArea.setText("Hi"+name+"...");
Or use the MessageFormat class:
messageArea.setText(MessageFormat.format("Hi {0} how are you?", name));
 
    
    
        Balázs Édes
        
- 13,452
- 6
- 54
- 89
- 
                    1
- 
                    1I am not talking about to do it using `+`. Read it [here](http://stackoverflow.com/questions/65668/why-to-use-stringbuffer-in-java-instead-of-the-string-concatenation-operator) and [here](http://stackoverflow.com/questions/4645020/when-to-use-stringbuilder-in-java) just for your learning and then update the post with your findings. – Braj Jun 05 '14 at 19:28
- 
                    Agreed, StringBuilder is a bit too much, but the rest of the answer is great. – hooknc Jun 05 '14 at 19:30
- 
                    1If you concatenate multiple strings, its always a good practice to use the mutable StringBuilder class instead of wasting resources with + concatenation. In this case it's really not much, but since OP looks like a beginner, it's good to hear about it. – Balázs Édes Jun 05 '14 at 19:30
- 
                    Generally, in JDK 1.6+, the compiler will change the ```+``` to use ```StringBuilder``` anyways. – David Yee Jun 05 '14 at 19:33