I guys and girls - im a MrScruff and found your forums when searching for some java problems. It looked like a nice place so i decided to join up! Heres my first java question which has had me perplexed for 2 days considering im sure it should be simple.

All i want to do is convert an array of ints - into one long string with spaces between each int (so i can tokenize this in a decryption method im making):

for(int h = 0;h<emptyArray.length;)
			 textArray[h] = emptyArray[h];
			 System.out.println("Char"+h+"]: "+textArray[h]);


		 //char[] cArray = .toCharArray();
		 //encryption = cArray.toString();
		 encryption = new String(tempArray);


		 return encryption;

This code is a bit messed up as i was wondering if i should convert it to a char Array first. But anyway Encryption is the string i want to return, emptyArray is the array of ints.

Any ideas and thanksfor any help - and i hope i posted ok :)

12 Years
Discussion Span
Last Post by jwenting

OK, let's say you have an array of integers called iArray, and you want to print this to a String tokenised with whitespace.

If you're using the latest JDK (5.0, 1.5, Tiger) it gets very easy as there's a new built-in function to turn an array of ints into a String.

int[] iArray = null;
// code to fill the array goes here...

String temp = Arrays.toString(iArray).replace(", ", " ");
String encoded = temp.substring(1, temp.length()-2);

As you see 2 lines of code do the trick of turning your array into a String which is formatted to your liking.

I've deliberately included a small error for you to fix in that code. You should be able to find it rather easily ;)


Integer.parseInt(String) will do the reverse of what's intended, it turns a String into an int.
Integer.toString(int) will turn an int into a String but won't create the String combination of integers with spaces interspersed.
It's also not needed in this case.
When not using Tiger simply iterate over the array and add the individual elements to the String with spacing like this

StringBuffer buf = new StringBuffer();
for (int i = 1; i < iArray.length; buf.append(" ").append(iArray(i++)));
String encoded = buf.toString();

Double the code of the Tiger implementation :)

Use what you prefer, under Tiger both are valid.
Haven't timed either, the StringBuffer implementation might be slightly faster and/or use less memory (especially in the String constant pool) which might make it the preferred choice for devices with severe memory restrictions.


Thankyou jwenting that worked a treat. I am not using Tiger, unfortunatly, and I had never thought of using StringBuffer - i tried using the concat function to stick the strings together but it never worked out.

Was the first delibrate mistake something to do with (","","") function? If not give please give me a hint :)


no, it has to do with the substring function. Check the arguments and see what would happen if you passed those...

StringBuffer is far more efficient than concatenating Strings. You use far less memory and CPU time.
Remember that Strings in Java are immutable.
If you do a = a + b where a and b are Strings it will not concatenate b to a, it will create a new String which is a and b concatenated and then assign that to a.
The concat() in String works the same way.
You probably tried to do a.concat(b) (where again a and b are Strings) and were surprised that a hadn't changed?
If you keep in mind that String is immutable that becomes obvious. What you did was create a new String consisting of a concatenated with b and then throw that String away.
Had you done a = a.concat(b) it would have worked but then you could just have said a = a + b which has the same effect and is highly inefficient if you do it a lot to the same String.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.