Results 1 to 5 of 5
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question Concatinating a String

    Just wondering is this the right way to go about creating a method that concatinates a string a certain number of times or is there a better way.

    It doesnt look right to me, wont this loop just pring one sentance because it will be returned once, the loop is not actually adding them together.

    Java Code:
       public String concat(String sentance, int count)
       {
    
           for (int i = 0; i < count; i++) {
               return sentance;
           }
           return sentance;
       }
    Thanx george
    -Long time no c-
    -:eek:-

  2. #2
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    If you want to concatenate the same string over and over your method will only return it once or probably keep returning it the same way if there is no string that was initialized in the main class to catch it. Anyway, this is what ur method is supposed to look like

    Java Code:
    public String conc(String sentence , int count ) {
    
    String s = null;      // creating an empty string
    
    for ( int i = 0 ; i < count ; i++ ) {
    
    s = s +sentence ;       // this will add ur string over over
    }
    
    return s;   // finally it has been combined into one string and will return it
    
    }

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    You are correct: that method only returns the original string. A simple way to concatinate a string would to place the following in the loop:
    Java Code:
    newSentance = newSentance + sentance;
    and also do the following:
    • Define the newSentance variables before the loop
    • remove the return statement in the loop

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Thanx both of you for the help.

    I changed the code and think this is the right idea:

    Java Code:
       public String concat(String sentance, int count)
       {
           String newSentance = null;
           for (int i = 0; i < count; i++) {
               newSentance += sentance;
           }
           return newSentance;
       }
    -Long time no c-
    -:eek:-

  5. #5
    Inaam is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    You can also use the StringBuffer and StringBuilder class to cancatinate the string.

    Code:

    StringBuffer buffer = new StringBuffer();

    buffer.append("abc");
    buffer.append("def");
    buffer.append("ghi");

Similar Threads

  1. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 02:05 PM
  2. Replies: 5
    Last Post: 01-11-2009, 11:12 AM
  3. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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