0

My problem is this. I need to make a String that is composed of letters 'z'. For example, if a user enters a word "goodbye" i want to display the String "zzzzzzz", for word "house" it would be "zzzzz" and so on. Can someone pls help me with this method.

4 Answers4

1

I would just use regex, take a look at this regex cheatsheet:

String myWord = "hello";

String newWord = myWord.replaceAll("[^\\d.]", "z");

// prints zzzzz
System.out.println(newWord);

So this will only switch out each letter for z in your output

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29
1

I assume u want to replace all user input with 'z' when a user inserts a letter you want to convert it into z and show it on edit text. then follow the code below.

et = findViewById(R.id.et);
et.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String s = new String ();
            for(int k = 0; k<charSequence.length(); k++){
            s = s+'z';
          }
          et.setText(s) //charSequence is user current input


        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
  • No, I want to add a method that passes a String as an argument and returnes String made of 'z' with the same length – Damjan Vlaic Jul 05 '18 at 22:10
0

There are a lot of ways to do this. See here and here. Here's an example of one way to do it that preserves the original string.

String makeZs(String in) {
    StringBuilder b = new StringBuilder();
    for(int i = 0; i < in.length(); ++i) {
        b.append("z");
    }
    return b.toString();
}
Tyler V
  • 9,694
  • 3
  • 26
  • 52
0

You can try this.

public String toZstring(String userString){

    String zString = "";

    for(int i = 0; i < userString.length(); i++){
        zString = zString + "z";
    }

    return zString;

}
J. Jefferson
  • 980
  • 8
  • 12