Results 1 to 7 of 7
  1. #1
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Removing characters in an array

    Hey guys,

    I'm working on a game (still) that is based upon a scrambled word that the user guesses. So far everything is working great except one thing. If the word has more than one of the same letter, whenever I type it, it adds all of the instances of that character. This is the code I'm using. What am I doing wrong??!?!

    Thanks in advance!!

    Java Code:
     public void keyTyped(KeyEvent e)
      {
          String s  =  Character.toString( e.getKeyChar( ));
          char c = e.getKeyChar( );
          System.out.println( s );
          Character myChar = new Character(c);
          char[] d = word.toCharArray();
    
          
          int x = word.length();
          
          if(myChar.toString().equals (" "))
          {
            current = "";   
          }
          else if (myChar.toString().equals("?"))
          {
            hintBox.show();
            score -=4;
          }
          else if (myChar.toString().equals("\n"))
          {
            score -=1;
            hintBox.hide();
            word = words.next();
            hint = hints.next();
            scrambledWord(word);
           }
                         
          else
          {
                  
          for(int i = 0; i<word.length(); i++)
          {
            
            if(c == d[i])
            {
              current = current + c;
              
            }
           
             
               else
              current +="";   
          }
          if(current.equals(word))
              {
                current = "";
                score += x;
                word = words.next();
                hint = hints.next();
                scrambledWord(word);
              }  
          }
          
         refreshDisplay();
      }
    Last edited by Norm; 11-26-2011 at 09:19 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Removing characters in an array

    Can you print out the values of the variables you are talking about and explain what is wrong with their value?

    Also a test program that compiles and executes and shows the problem would help. Your bit of code with no supporting documentation is not too useful.
    For example if you add lots of printlns to the code that shows how the values of the variables change as the program executes, the output from those printlns would show what is happening in the code.

  3. #3
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Removing characters in an array

    Well when I add println to the code for the variables it just shoots out tons of what I type in. The System.out.println(s); line up at the top prints out the letter I type in, and even when I try and add that single line to the code it adds all the instances. It's very confusing.

    The problem I'm having up above is this:

    Java Code:
    for(int i = 0; i<word.length(); i++)
    {

    if(c == d[i])
    {
    current = current + c;

    }


    else
    current +="";
    }

    Where word is equal to anything with multiple instances of a single letter, say "Primitive".

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Removing characters in an array

    Please add printlns to the code to show the input and the output from the code you are having problems with.
    Copy the output here to show what the code does.

  5. #5
    Join Date
    Nov 2011
    Location
    Northern Virginia, USA
    Posts
    9
    Rep Power
    0

    Post Re: Removing characters in an array

    Try adding this at line 39:

    Java Code:
    break;
    This will cause program execution to transfer to line 46, instead of continuing to run through all the letters still left in your d array, which is why I think it is adding all matches instead of just one. Note, however, that unless you removed the matched letter from your target word after it is added to current, it will match again and again, as many times as your user enters that letter. To stop that from happening, you will need to copy your target word to a new buffer, minus the matched letter. One way to do this would be to copy as you compare in your for loop, then add a second for loop that skips the matched letter, and copies the remaining letters. The copy becomes the word you use to create your d array in subsequent runs of the code you posted.

  6. #6
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Removing characters in an array

    Thank you a ton Steven, so simple and yet it took me hours of searching and you only a second to point it out.

    My program runs wonderfully, I am very very thankful!!!

    -dru36

  7. #7
    Join Date
    Nov 2011
    Location
    Northern Virginia, USA
    Posts
    9
    Rep Power
    0

    Smile Re: Removing characters in an array

    So glad I could help!

    By the way, I love your curly-bracket formatting. You do it the way I do it, like this:

    Java Code:
    if (<condition>)
    {
        <statement>
    }
    else
    {
        <statement>
    }
    as opposed to

    Java Code:
    if(<condition>) {
        <statement>
    } else {
        <statement>
    }
    I find your scheme much easier to read (and debug!), no matter what Sun says.

    Cheers!
    Last edited by stevensrmiller; 11-27-2011 at 01:03 PM.

Similar Threads

  1. Removing escaped characters from strings
    By Opid in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 01:42 AM
  2. Removing an Item from an array
    By chris1 in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 06:59 AM
  3. Removing indexes from an array.
    By Cruncher in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 09:41 PM
  4. removing 0 from the begining of the array
    By Hayzam in forum New To Java
    Replies: 7
    Last Post: 08-21-2008, 10:02 PM
  5. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM

Tags for this Thread

Posting Permissions

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