Search code examples
javascriptandroidstringkeystroke

Adding up letters to form words


Not sure I worded my question correctly but I tried.

I currently have key strokes being recorded within some JavaScript and have converted to them to their character values. I have also exported the variable that holds the letter into java.

   public void receiveKeyStroke(String keyStroke){
       Log.i(TAG, keyStroke);

    }

My question is how do I add the letter to another Variable eg a String and have them add on each time. The idea is if I type dog in the key pad, dog will then be in this variable that is created.


Solution

  • It seems that your keyStroke is being received as a String too. You can use a static variable inside the function receiveKeyStroke and append the incoming keyStroke every time the function is called. Static variables retain their values during multiple calls. For example, the variable word will contain the complete word below.

    public void receiveKeyStroke(String keyStroke){
       static String word;
       word = word + keyStroke;
       Log.i(TAG, keyStroke);
    }
    

    This code might not be efficient though and you can improve it by changing String to StringBuffer and/or using a class level variable instead of a static one.