Skip to content
Advertisement

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:

JavaScript

And I print it like this:

JavaScript

Advertisement

Answer

Basically, don’t use ArrayList.toString() – build the string up for yourself. For example:

JavaScript

(Personally I wouldn’t call the variable publicArray when it’s not actually an array, by the way.)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement