Results 1 to 4 of 4
  1. #1
    igniteflow is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Unhappy JAVA: String char removal with nested loop

    I'm trying to make a nested loop which will search for and remove chars from a string. The keyword contains six letters, alphabet of course is 26. Can anyone show me what I'm doing wrong? The second loop works fine to remove one char, but I need it to remove all six, so that only the letters of the alphabet remain that are not in the keyword.
    Java Code:
    for (int j = 0; j < keyword.length(); j++)
            letter = keyword.charAt(j);
             for (int i = 0; i < alphabet.length(); i ++)
                if (alphabet.charAt(i) != letter) r += alphabet.charAt(i);

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Suggestions...

    • Well... the first thing that stands out is a total lack of curly brackets "{}". Even if you have a one-liner after a "for" or "if", it's good to use them... you avoid headaches later on.

    Java Code:
    for (int j = 0; j < keyword.length(); j++)
        [COLOR="Red"]{[/COLOR]
            letter = keyword.charAt(j);
             for (int i = 0; i < alphabet.length(); i ++)
                 [COLOR="red"]{[/COLOR] 
                   if (alphabet.charAt(i) != letter) 
                    [COLOR="red"]{[/COLOR]
                      r += alphabet.charAt(i);
                    [COLOR="red"]}[/COLOR]
                 [COLOR="red"]}[/COLOR]
        [COLOR="red"]}[/COLOR]
    • Next, you're comparing strings using equality operators (==, !=). Since you're comparing strings it's better to use the String class methods like .equals() or .compareTo():

    String (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    igniteflow is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks, that's useful. Sorry I forgot to mention that "letter" is a char so I can't use the String API methods, I think.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default it would be useful...

    You are correct, you're comparing chars. It would probably be better if you posted all your code.
    For example:
    • What type/object is variable r?


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 4
    Last Post: 09-05-2008, 05:47 PM
  2. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 02:51 PM
  3. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-21-2008, 12:00 AM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 02:42 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:11 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
  •