Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How do I combine two strings in an array?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I combine two strings in an array?

    ea:
    Array[1]="Hi I'm ";
    Array[2] = "Paul";
    Array[3] = "RAGGLE";
    Array[4] = "FRAGGLE";

    I want to push "HI I'm Paul" and "RAGGLE FRAGGLE" so the array looks like this when I'm done.

    Array[1] = "Hi I'm Paul";
    Array[2] = "RAGGLE FRAGGLE";


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I combine two strings in an array?

        public String[] combine(String[] array){
            StringBuilder sb = new StringBuilder();
            String[] combined = {};
            for(int i = 0; i < array.length/2; i + 2){
                    sb.add(array[i] + array[i++);
                    combined[i/2] = sb.toString();
            }
            return combined;
        }

    I'm not completely sure but I believe this would be how to do that the methods may not be the correct name though.

    Edit: Nvm, you would have to also clear the data in the sb so everything wouldn't just be smashed together.
    Last edited by Gamerv08; September 14th, 2011 at 09:30 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I combine two strings in an array?

    I want to push "HI I'm Paul" and "RAGGLE FRAGGLE" so the array looks like this when I'm done.
    Use assignment statements and concatenation.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I combine two strings in an array?

    I see what needs to be done. Thanks dude.

Similar Threads

  1. Adding strings to an array dynamically, how can i do it?
    By emkoirl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2011, 02:07 PM
  2. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  3. filling an array with strings
    By exose in forum Collections and Generics
    Replies: 3
    Last Post: February 18th, 2011, 09:44 AM
  4. How to combine these 3 void methods?..
    By TimoElPrimo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 8th, 2011, 02:25 PM
  5. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM