Results 1 to 17 of 17
Thread: Concatenation with strings
- 04-27-2011, 06:50 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Concatenation with strings
I am trying to perform a concatenation of seven strings to form a sentence but the Java compiler says cannot be applied to... here is my code
public class StringSentence
{
public static void main(String[] args)
{
String str1 = "This";
String str2 = " sentence";
String str3 = " is";
String str4 = " written";
String str5 = " using";
String str6 = " seven";
String str7 = " strings!";
System.out.println("The combination of these strings is: " + str1+concat(str2, str3, str4, str5, str6, str7));
}
}
- 04-27-2011, 06:56 PM #2
What is this concat() method you speak of?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-27-2011, 07:16 PM #3
It's as easy as a plus (+) sign :D
Java Code:public class StringSentence { public static void main(String[] args) { String str1 = "This"; String str2 = " sentence"; String str3 = " is"; String str4 = " written"; String str5 = " using"; String str6 = " seven"; String str7 = " strings!"; System.out.println("The combination of these strings is: " + str1+" "+str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7); } }
- 04-27-2011, 07:36 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Bear in mind that this kind of concatenation is very inefficient with the String class - not crucial in a one-off method like this, but for real-world applications where concatenation is used a lot, consider using StringBuilder instead.
- 04-27-2011, 07:38 PM #5
this is true
- 04-27-2011, 07:40 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 78
- Rep Power
- 0
once again you have judged correctly.
- 04-28-2011, 01:27 AM #7
I've heard that before, but is that really true? From the String API:
String concatenation is implemented through the StringBuffer class and its append method.
Is a StringBuffer really that much less efficient than a StringBuilder?
Ah, I see, I've answered my own question by reading the StringBuffer API:
The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
Anyway, I'll still post this because it's slightly informative.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-28-2011, 01:45 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
No, you're quite right - although StringBuilder may indeed be a bit faster than StringBuffer, I was misremembering the admonition against String concatenation in loops - where the String is modified every time round - as discussed in this old article and its comments.
So there it is - the mistake I couldn't promise I wouldn't make ;)
Time for bed.
- 04-28-2011, 03:19 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Wow. It's kind of funny how my initial thread just completely turned around. Anyway, thanks sehudson. The plus signs worked! Not sure if my java teacher will care. He didn't specify to use a certain method. Now I'm on to String Buffer!:)
- 04-28-2011, 03:42 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The way I learned the difference is when reading thinking in java, they used javap to decompile the code and the produced instructions showed about half as many when using string builder instead of using the + operator.
- 04-28-2011, 10:10 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
It's String concatenations in a loop that are the killer because a new, longer String must be allocated each time around...
- 05-03-2011, 02:57 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
This might be a stupid question but is it possible to store a set of Strings in a ArrayList and if so, how?
- 05-03-2011, 04:34 PM #13
How about add(E e) method of ArrayList?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-05-2011, 06:02 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
So this is what I have so far:
import java.util.*;
public class StringSentence
{
public static void main(String[] args)
{
ArrayList strings = new ArrayList();
String str1 = "This";
String str2 = " sentence";
String str3 = " is";
String str4 = " written";
String str5 = " using";
String str6 = " seven";
String str7 = " strings!";
strings.add(E str1);
strings.add(E str2);
strings.add(E str3);
strings.add(E str4);
strings.add(E str5);
strings.add(E str6);
strings.add(E str7);
System.out.println("The combination of these strings is:" + strings);
}
}
It doesn't like the space after the E of the ) at the end.
- 05-05-2011, 06:06 PM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You don't add the E, the E specifies the type of argument add takes. E is the parametrized type of the list. So when you call the actual method it would look like this
Also, if you want to use an array list you should parameterize it. To do this, add the type you want the list to score in brackets. Like this,Java Code:add(/*args*/);
this is a list where each element is a string.Java Code:ArrayList<String> arr = new ArrayList<String>();
Last edited by sunde887; 05-05-2011 at 06:08 PM.
- 05-05-2011, 07:17 PM #16
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
You're makiing it harder than it really is. You could quite easily do this:
Java Code:String sentence = str1+str2+str3+str4+str5+str6+str7;
- 05-05-2011, 07:33 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Need help with Strings
By </3java in forum New To JavaReplies: 24Last Post: 02-17-2011, 05:00 PM -
Accessing array index through the result of a string concatenation
By colpwd in forum New To JavaReplies: 4Last Post: 08-19-2010, 01:06 AM -
Concatenation file contents
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2008, 01:29 PM -
Database Column vs String Concatenation
By varun_33 in forum JDBCReplies: 1Last Post: 12-11-2007, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks