Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By ezkatz19

Thread: Issue With Removing Characters

  1. #1
    ezkatz19 is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Issue With Removing Characters

    My code is supposed to remove all of the repeated letters in Keyword1 and rewrite the String as Keyword12 (without any repeats), but it's not working.
    Here's my code:

    public static void main(String [] args){
    // Code goes here

    Scanner scan = new Scanner(System.in);

    System.out.println("Write first keyword");

    String Keyword1 = scan.nextLine();
    String Keyword12 = Keyword1.substring(0,1);
    for(int x = 1; x<Keyword1.length(); x++){
    boolean b = true;
    for(int y=0; y<Keyword12.length(); y++){
    if (Keyword1.charAt(x) == Keyword12.charAt(y))
    b=false;
    }
    if (b=true)
    Keyword12 = Keyword12+Keyword1.charAt(x);
    System.out.println(Keyword12);
    }

    System.out.println(Keyword12);

    }

    How come when I type in the word apple, it returns "apple" instead of "aple"?
    Thanks a bunch!

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Issue With Removing Characters

    If it always adds the letters, check and make sure your boolean is being set and assigned correctly. Throw in some println(b) statements to double check b's value is what you think it should be in your if statements.

  3. #3
    ezkatz19 is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue With Removing Characters

    ahh I figured it out, it was because my if(b=true) should have been if(b==true)

    Thanks, Shoss!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Issue With Removing Characters

    Quote Originally Posted by ezkatz19 View Post
    ahh I figured it out, it was because my if(b=true) should have been if(b==true)

    Thanks, Shoss!
    No. It should be
    Java Code:
    if (b) {
    //
    }
    Comparing a boolean to true or false is pointless, redundant and leads to just the mistake you experienced. Lose the habit and test the boolean itself, not its equivalence to true -- which just adds a useless step.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ezkatz19 is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue With Removing Characters

    Ahh. I've been learning Java for just over a month, so cut me some slack, I didn't know that you could do that. Don't be a jackass and command, be suggestive.

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

    Default Re: Issue With Removing Characters

    Quote Originally Posted by ezkatz19 View Post
    Ahh. I've been learning Java for just over a month, so cut me some slack, I didn't know that you could do that. Don't be a jackass and command, be suggestive.
    He is suggesting not demanding, and someone who is not quite so thin-skinned would have appreciated his taking an interest in furthering your Java education, would have thanked him for the help rather than antagonize him, and any others who read your remarks and who might have helped you in the future. Nice going there bucko.

  7. #7
    ezkatz19 is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Issue With Removing Characters

    You're right. I'm mad at something else right now, and I was wrongfully taking it out on you, DarrylBurke. I just didn't like the way you said "No. It should be" and "Lose."
    Thanks, both.
    Fubarable likes this.

Similar Threads

  1. Removing characters in an array
    By dru36 in forum New To Java
    Replies: 6
    Last Post: 11-27-2011, 12:56 PM
  2. Removing escaped characters from strings
    By Opid in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 01:42 AM
  3. Replies: 6
    Last Post: 09-05-2011, 01:25 PM
  4. XHTML to FO Transformation Issue for Special Characters
    By simsu123 in forum Advanced Java
    Replies: 1
    Last Post: 06-30-2010, 12:04 AM
  5. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM

Posting Permissions

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