1

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;

            }

        });
user3242773
  • 23
  • 1
  • 5
  • Use '\b' for Backspace – Suzon Feb 21 '14 at 17:23
  • just put the delete after the loop... – njzk2 Feb 21 '14 at 17:27
  • i tried putting the delete after the loop and it still does not work.. and i guess one of the problem is the way i use deleteCharAt(), cause i read that from here"http://stackoverflow.com/questions/5212928/how-to-trim-a-java-stringbuilder" that the deleteCharAt method copies the array over every time, so i think thats the reason why it does not work.. i hope someone can provide another method if that is indeed the case – user3242773 Feb 21 '14 at 17:33

2 Answers2

1

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());
    }
}     
Danny Nimmo
  • 674
  • 7
  • 22
0

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.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22