CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2011
    Posts
    13

    [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

  2. #2
    Join Date
    Mar 2011
    Posts
    13

    Resolved 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;
             
       }

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Mar 2011
    Posts
    13

    Re: [RESOLVED] String problem.

    Quote 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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Mar 2011
    Posts
    13

    Re: [RESOLVED] String problem.

    Quote Originally Posted by keang View Post
    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: [RESOLVED] String problem.

    I was so exited when the code worked...
    Yep, it's a great feeling when your code works.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured