Results 1 to 14 of 14
Thread: Recombining Strings
- 05-05-2009, 12:46 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
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:
- 05-05-2009, 12:54 AM #2
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
- 05-05-2009, 01:02 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
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.
- 05-05-2009, 01:18 AM #4
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
- 05-05-2009, 03:01 AM #5
You could just try your idea out first and see if it gets you what you want.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.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-05-2009, 03:30 AM #6
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
I know that obviously, but I don't know what to type to get the 2 different halves.
-
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.
- 05-05-2009, 04:38 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
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);
}
- 05-05-2009, 04:39 AM #9
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-05-2009, 05:34 AM #10
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
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");
}
}
-
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")); } }
-
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.if you're going to post then at least be helpful
Please let's all hold hands and sing "Kumbaya."Last edited by Fubarable; 05-05-2009 at 06:22 AM.
- 05-05-2009, 01:20 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 39
- Rep Power
- 0
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.
- 05-05-2009, 07:00 PM #14
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
-
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Reading in strings
By thekermo in forum New To JavaReplies: 2Last Post: 10-19-2008, 05:24 PM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks