I'm porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits my Android layout.
How do I print an array without the brackets and commas? The array contains slashes and gets replaced one-by-one when the correct letter is guessed.
I am using the usual .toString()
function of the ArrayList
class and my output is formatted like: [ a, n, d, r, o, i, d ]
. I want it to simply print out the array as a single String
.
I fill the array using this bit of code:
List<String> publicArray = new ArrayList<>(); for (int i = 0; i < secretWordLength; i++) { hiddenArray.add(secretWord.substring(i, i + 1)); publicArray.add("-"); }
And I print it like this:
TextView currentWordView = (TextView) findViewById(R.id.CurrentWord); currentWordView.setText(publicArray.toString());
Any help would be appreciated.
To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.
You can print a set without brackets by combining the string. join() method on the separator string ', ' with a generator expression to convert each set element to a string using the str() built-in function. Specifially, the expression print(', '.
Replace the brackets and commas with empty space.
String formattedString = myArrayList.toString() .replace(",", "") //remove the commas .replace("[", "") //remove the right bracket .replace("]", "") //remove the left bracket .trim(); //remove trailing spaces from partially initialized arrays
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With