how to make a backspace button that removes the last character of a stringbuilder inside a for loop

StackOverflow https://stackoverflow.com/questions/21940994

  •  14-10-2022
  •  | 
  •  

Question

I am making an Android app that receives different level of voltages from arduino to the android via bluetooth and translate it into letters. My app can already translate it into letters, and my problem now is I dont know how to delete the last letter in the stringbuilder which is inside a for loop.. I have this button called backspace and i want it to work with the same function like in a computers's backspace wherein pressing it will delete the last letter.. I hope someone can help me, im still new to this.

note: these codes are from the Main Activity; sbletter is a stringbuffer that appends letters

boolean test = false;
char[] charArray = sbletter.toString().toCharArray();
char currentletter =' ';
char prevletter =' '; 

StringBuilder strBuild1 = new StringBuilder();

    for(int i = 0; i < charArray.length; i++) {
        currentletter = charArray[i];
            if(curletter != prevletter) {
                strBuild1.append(charArray[i]);
                    if(test){
                       strBuild1.deleteCharAt(strBuild1.length()-1); 
                    }
                test = false;
            }
                prevletter = currentletter;
        }

SecondActivity.textView1.setText(strBuild1.toString());

SecondActivity.backspace.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                test = true;

            }

        });
Was it helpful?

Solution 2

The onClick method in the onClickListener of the backspace is event driven, i.e., when the backspace button will be clicked, the onClick method will be called. So setting test to true there won't matter as the for loop would have already stopped executing by that time, since its outside somewhere.

You can directly use sbletter to delete its last character like following:

backspace.setOnClickListener(new View.OnClickListener() {

  public void onClick(View view) {

    sbletter = sbletter.deleteCharAt(sbletter.length - 1);

    // Now the last character from sbletter is deleted.
    // Use sbletter for whatever purpose you like now.
  }
}

Note that deleteCharAt method of StringBuilder and StringBuffer return StringBuilder and StringBuffer objects respectively which carry with them the changed value, that's why I wrote:

sbletter = sbletter.deleteCharAt(sbletter.length - 1); // Will work as expected.

and not just:

sbletter.deleteCharAt(sbletter.length - 1); // Won't work as expected.

Hope that helps.

OTHER TIPS

This works for sure:

private void Zero1ActionPerformed(java.awt.event.ActionEvent evt) {                                      

    if (OutputTextField.getText().length()>0){
        StringBuffer sb = new StringBuffer(OutputTextField.getText());
        sb = sb.deleteCharAt(OutputTextField.getText().length()-1);
        OutputTextField.setText(sb.toString());
    }
}     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top