Results 1 to 14 of 14
  1. #1
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default Recombining Strings

    Hi, I am a little confused on what to do. I'm supposed to recombine 2 words like this... take the first half of word1, take the second half of word2, then concatenate the two halves and return the new string. So, if word 1 was "apple" and word2 was "pear" then it would come out to "apar". Then if word1 was "pear" and word2 was "apple" then it would come out to "peple". I was thinking of doing something like getting the length of word1, dividing it by 2, then taking the first half of it and doing the same to word2 except taking the second half and adding them together. Or maybe I should use subString. I'm confused :confused:

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Did you try doing that?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Yes, but I don't understand how I'm supposed take the first and second halves of each word so that I can concatenate them together.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Exactly as you said.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I was thinking of doing something like getting the length of word1, dividing it by 2, then taking the first half of it and doing the same to word2 except taking the second half and adding them together. Or maybe I should use subString.
    You could just try your idea out first and see if it gets you what you want.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    I know that obviously, but I don't know what to type to get the 2 different halves.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There's nothing obvious here. You still have a question, and thus I think it is still up in the air exactly what you know here and what you don't. I agree with the others, that you should still try to apply what you know to create a small method to do this and then post the code.

    In other words, please allow your code to show us what you do and what you don't know, and we'll be better able to help you.

  8. #8
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Ok, here's my code. It's pretty much nothing because like I said, I don't know how I'm supposed to get the 2 halves.

    private String recombine(String word1, String word2)
    {
    int a = (word1.length() / 2);
    int b = (word2.length() / 2);
    }

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by AWPtic View Post
    I should use subString
    .................................................. ............................
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Here's my updated code and if you're going to post then at least be helpful. I have an AP Exam tomorrow morning and I need to know how to do this problem. Right now it's putting the 2 full words together. So, it's saying "applepear" as the result.

    public class Main
    {
    private static String recombine(String word1, String word2)
    {
    int a = (word1.length() / 2);
    int b = (word2.length() / 2);
    String result = "";

    word1.substring(0, a);
    word2.substring(b, word2.length());
    result = word1 + word2;
    return result;
    }
    public static void main(String[] args)
    {
    recombine("apple", "pear");
    }
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Strings are immutable. if you do this:
    word1.substring(...) you won't change word1 at all. instead do:
    word1 = word1.substring(....);

    i.e.,
    Java Code:
    public class MainFu
    {
      private static String recombine(String word1, String word2)
      {
        int a = (word1.length() / 2);
        int b = (word2.length() / 2);
        String result = "";
    
        word1 = word1.substring(0, a);
        word2 = word2.substring(b, word2.length());
        result = word1 + word2;
        return result;
      }
    
      public static void main(String[] args)
      {
        System.out.println(recombine("Apfelstrudel", "inapeartree"));
      }
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    if you're going to post then at least be helpful
    Please be very very careful here. These posts of ours (all done by volunteers on their free time, mind you ) have been helpful. Denigrating them will not serve you well, as this may not help motivate others to put in further effort here in the future. If you have a specific beef with one of the posts, then you should address it in an as non-confrontational way as possible. It could all be due to a simple misinterpretation or misunderstanding by one part or the other.

    Please let's all hold hands and sing "Kumbaya."
    Last edited by Fubarable; 05-05-2009 at 06:22 AM.

  13. #13
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Lol, well I don't know about the holding hands and singing Kumbaya part, but ok on the rest. It's just annoying when I'm trying to figure something out for 5 hours and no one will post anything except "........." or tell me to use my idea, because seriously... I wouldn't be asking for help if my idea had worked in the first place. Nice class name btw, lol.

  14. #14
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I was trying to only post your quote, but post limits required me to make it longer. You'd already been told 3 times that what you'd said was correct, but had yet to produce an attempt to use it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  2. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM
  3. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM

Posting Permissions

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