TheEngineeeeeeer
TheEngineeeeeeer

Reputation: 49

How can I limit the string builder length

I am working on a calculator app just like the phones have, I am taking the numbers with buttons. I am using a string builder, when user presses to a new button string appends. Just like when you press button2 it adds number "2" to the string, then you press 5, so the new string is "25" and it goes like this and shows the string on a textView there is no problem. The problem is I want to limit the string builder like 8 digits. I limit the textView digit number but can not do it for the string builder. TextView has 8 digit restriction, so it does not show the 9th value but string accepts the 9th value. I used some codes from developer.android but can not find a solution. How can I limit the string builder length?

The code for limiting the textView;

int maxLength = 8; //at the top of the class

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(maxLength);
myTv.setFilters(filters);

The code; tried to limit the StringBuilder length;

public StringBuilder myStbuilder;

myStbuilder= new StringBuilder(); myStbuilder.setLength(maxLength);

Upvotes: 2

Views: 901

Answers (3)

Kawser Habib
Kawser Habib

Reputation: 1422

StringBuilder class is declared final, so it can't be subclassed. An alternative is to create a customized version that supports limiting the size.

CustomStringBuilder class:

final class CustomStringBuilder{
    private StringBuilder sb;
    private final int maxLength;
    
    CustomStringBuilder(int maxLength){
        sb = new StringBuilder();
        this.maxLength = maxLength;
    }
    
    public <T> void append(T val){
        if(sb.length() < maxLength)
            sb.append(val);
        else
            System.out.println("Warning: StringBuilder max length overflow");
    }
    
    public int length() {
        return sb.length();
    }
    
    public int capacity() {
        return sb.capacity();
    }
    
    public String toString() {
        return sb.toString();
    }   
    
    // implement necessary method
}

Modified version of your code:

public class StringBuilderLimit {

    public static void main(String[] args) {
                int maxLength = 8;
        CustomStringBuilder sb = new CustomStringBuilder(maxLength);
        sb.append(0);
        sb.append(1);
        sb.append(2);
        sb.append(3);
        sb.append(4);
        sb.append(5);
        sb.append(5);
        sb.append(7);
        sb.append(7);
        
        System.out.println(sb.length()+ "," + sb.toString());
    }
}

If you try to add more than capacity, then it will show a warning message.

Warning: StringBuilder max length overflow

Upvotes: 2

akhil nair
akhil nair

Reputation: 1561

A better approach would be to have a common listener for all the buttons or a common method inside the listeners of all the buttons and then check length. This way you could also show the user some Toast which would be nice from an end user perspective.

StringBuilder numberEnteredByUser = new StringBuilder();

public void numberClicked(String number){
  if(numberEnteredByUser.length()<8) {
     numberEnteredByUser = numberEnteredByUser.append(number);
  }
  else {
    //show toast
  }
}

Upvotes: 3

Stitchie
Stitchie

Reputation: 1

new StringBuilder(8) should work.

Upvotes: -2

Related Questions