Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print array without brackets and commas

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.

like image 200
SQDK Avatar asked Dec 08 '10 15:12

SQDK


People also ask

How do you print an array without brackets?

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.

How do I print a set without brackets in Python?

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(', '.


1 Answers

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 
like image 196
user489041 Avatar answered Sep 17 '22 12:09

user489041