Results 1 to 17 of 17
  1. #1
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    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!

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

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

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    this is true

  6. #6
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    once again you have judged correctly.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Quote Originally Posted by dlorde View Post
    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.
    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!

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by KevinWorkman View Post
    ...
    Anyway, I'll still post this because it's slightly informative.
    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.

  9. #9
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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!:)

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    It's String concatenations in a loop that are the killer because a new, longer String must be allocated each time around...

  12. #12
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    This might be a stupid question but is it possible to store a set of Strings in a ArrayList and if so, how?

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by link6790 View Post
    This might be a stupid question but is it possible to store a set of Strings in a ArrayList and if so, how?
    How about add(E e) method of ArrayList?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  14. #14
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    add(/*args*/);
    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:
    ArrayList<String> arr = new ArrayList<String>();
    this is a list where each element is a string.
    Last edited by sunde887; 05-05-2011 at 06:08 PM.

  16. #16
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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;

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,565
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Solarsonic View Post
    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;
    Please read the thread before you reply; pay special attention to reply #4.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 05:00 PM
  2. Replies: 4
    Last Post: 08-19-2010, 01:06 AM
  3. Concatenation file contents
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2008, 01:29 PM
  4. Database Column vs String Concatenation
    By varun_33 in forum JDBC
    Replies: 1
    Last Post: 12-11-2007, 04:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •