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.
Asked
Active
Viewed 110 times
4 Answers
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) {
}
});

Shahadat Hossain Shaki
- 796
- 7
- 15
-
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
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
-
It's for android. To get the text the user typed out. I removed it since it's not relevant to the question. – J. Jefferson Jul 05 '18 at 19:59
-