Conver int Array into a String

Reply

Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Conver int Array into a String

 
0
  #1
Nov 17th, 2004
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):

  1. for(int h = 0;h<emptyArray.length;)
  2. {
  3. textArray[h] = emptyArray[h];
  4. System.out.println("Char"+h+"]: "+textArray[h]);
  5.  
  6. h++;
  7. }
  8.  
  9. //char[] cArray = .toCharArray();
  10. //encryption = cArray.toString();
  11. encryption = new String(tempArray);
  12.  
  13. System.out.println(encryption);
  14.  
  15. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Conver int Array into a String

 
0
  #2
Nov 17th, 2004
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.
  1. int[] iArray = null;
  2. // code to fill the array goes here...
  3.  
  4. String temp = Arrays.toString(iArray).replace(", ", " ");
  5. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 18
Reputation: Catnip is an unknown quantity at this point 
Solved Threads: 0
Catnip's Avatar
Catnip Catnip is offline Offline
Newbie Poster

Re: Conver int Array into a String

 
0
  #3
Nov 17th, 2004
Have you ever used 'ParseInt'? Here is the Java page on it, but if you need it to keep it's INT properties, it may not be what your looking for.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Conver int Array into a String

 
0
  #4
Nov 17th, 2004
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
  1. StringBuffer buf = new StringBuffer();
  2. buf.append(iArray[0]);
  3. for (int i = 1; i < iArray.length; buf.append(" ").append(iArray(i++)));
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Re: Conver int Array into a String

 
0
  #5
Nov 17th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Conver int Array into a String

 
0
  #6
Nov 17th, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC