'Changing a string with each button press repeatedly

TL;DR: I'm searching for a way to change a string of 6 characters repeatedly with each button press, so that at the end of the string appears new value, and starting value gets deleted for the string to stay 6 char in length.

I'm trying to write an app with alarms, stopwatch and a countdown timer. So far writing the timer portion i can't wrap my head around this:

I have a numpad made of buttons, like you would have in a calculator app. I want each button to add its value to the end of 6 char length string. The String has a starting value of "000000", from which three other strings take each 2 chars from left to right(and add "h ","m "and "s") so i can display it as "00h 00m 00s". The value of the string would be converted into milisec when it's accepted by the user starting a countdown. I have button presses connected to OnClick method with a switch, and there individual buttons send different values to the countDownTimer method as "input". And my EditText is timerValue.

public class TimerApp extends AppCompatActivity implements View.OnClickListener {
    String valueOfTimer = "000000"

    ...

    public String countDownTimer(String input){
        StringBuilder contN = new StringBuilder(valueOfTimer);
        StringBuilder countdown = new StringBuilder(6);
        countdown.append(contN);

        int inputLgt = input.length();
        if (inputLgt == 1) {
            countdown.delete(0, 1);
        }
        else if(inputLgt == 2){
            countdown.delete(0,2);
        }

        countdown.append(input);;
        contN.replace(0,7, String.valueOf(countdown));

        EditText cursor = (EditText)findViewById(R.id.timerValue);
        cursor.setSelection(timerValue.getText().length());

        String h = countdown.substring(0, 2) + "h ";
        String m = countdown.substring(2, 4) + "m ";
        String s = countdown.substring(4, 6) + "s";
        String hms = h+m+s;

        timerValue.setText(hms);
        return countdown.toString();
    

So order of operation in my mind:

  • press the button (switch gets R.id. and sends e.g. 1 to the countDownTimer method
  • the method takes this value, checks for length (i have a button which sends "00") and deletes first [length-amount] chars in the "000000"
  • StringBuilder appends the input
  • StringBuilder value is changed, and should be stored somewhere somehow (i don't understand how to do this part)
  • this value is displayed in the EditText on the screen to the user
  • changed value e.g. "00001" is ready to be treated again in the same way as above
  • you press 3, value should be "000013" and so on

The problem is when i press e.g. "1", it shows "00h 00m 01s", and if i press anything again, it only changes the last char to whatever i press.(e.g. i press "2" after "1" it shows "00h 00m 02s" and not "00h 00m 12s")

This countDownTimer i have tried making with

stingName = stringName.replaceFirst(String.valueOf(stringName.chatAt(0)), "");
stringName = stringName + buttonValue

and with StringBuilder and StringBuffer, but nothing works.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source