-
March 15th, 2011, 09:38 AM
#1
[RESOLVED] String problem.
Hello All,
here is the code I have came out with, but it doesn't work as it should.
I have tried to get it working correctly but now I have ran out of ideas.
Code:
public static String lastTranslate(String aString)
{
String[] choppedString = new String[aString.length()];
String delimiter = " ";
String result = new String();
choppedString = aString.split(delimiter);
for (int i = 0; i<choppedString.length; i++)
{
result = result + firstTranslate(choppedString[i]);
if (result.contains(choppedString[i]))
{
result = result + secondTranslate(choppedString[i]);
}
}
return result;
}
I have checked firstTranslate and secondTranslate and they seem to work ok.
I have problem to get lastTranslate to work. for example if aString will be "2 y" the result I'm getting is 2towhyy instead of towhy. when aString is "2 idk" it will produce 2toI don't know".
Don't know why but when output string for firstTranslate is longer than 3 characters it is produced correctly.
Please can someone push me in the right direction.
Regards
Intruz
-
March 15th, 2011, 01:25 PM
#2
Re: String problem.
I've spent few more hours playing with this code and finally it works as it should
Code:
public static String lastTranslate(String aString)
{
String[] choppedString = new String[aString.length()];
String delimiter = " ";
String[] result = new String[aString.length()];
choppedString = aString.split(delimiter);
String complete = new String();
for (int i = 0; i<choppedString.length; i++)
{
result[i]= firstTranslate(choppedString[i]);
if (result[i]==(choppedString[i]))
{
result[i]=secondTranslate(choppedString[i]);
}
complete = complete+result[i];
}
return complete;
}
-
March 15th, 2011, 04:05 PM
#3
Re: [RESOLVED] String problem.
Good to hear you solved it for yourself.
Some comments about your code:
- You don't need to assign a String array to choppedString where you declare it as you are assigning the String array returned by split() to it.
- You don't need result to be an array and you only need to declare it inside the for loop. You also don't need to assign a String object to it when you declare it.
- Concatenating strings in a loop is generally bad practice. You should use a StringBuilder object instead of concatenating result with complete.
-
March 15th, 2011, 05:39 PM
#4
Re: [RESOLVED] String problem.
Originally Posted by keang
Some comments about your code:
You don't need to assign a String array to choppedString where you declare it as you are assigning the String array returned by split() to it.
You don't need result to be an array and you only need to declare it inside the for loop. You also don't need to assign a String object to it when you declare it.
Concatenating strings in a loop is generally bad practice. You should use a StringBuilder object instead of concatenating result with complete.
Thanks for pointing bad bits in my code, preciate your help.
I'll try to correct it.
-
March 15th, 2011, 05:44 PM
#5
Re: [RESOLVED] String problem.
Another thing, which I can't believe I didn't spot before, is in your if statement. You should never compare strings using str1 == str2, you should always use str1.equals(str2). Unless, of course, you really want to test to see if they are referencing the same object rather than having the same contents.
-
March 15th, 2011, 06:39 PM
#6
Re: [RESOLVED] String problem.
Originally Posted by keang
Another thing, which I can't believe I didn't spot before, is in your if statement. You should never compare strings using str1 == str2, you should always use str1.equals(str2). Unless, of course, you really want to test to see if they are referencing the same object rather than having the same contents.
Thanks again.
I was so exited when the code worked that I didn't check it properly.
-
March 16th, 2011, 04:57 AM
#7
Re: [RESOLVED] String problem.
I was so exited when the code worked...
Yep, it's a great feeling when your code works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|