Link to home
Start Free TrialLog in
Avatar of AJThomas
AJThomas

asked on

String Array to String Conversion Help

I am trying to convert a String array, that  holds numbers as Strings, into a String that I can then compare with another string later in the code - any help would be gratefully received
Avatar of sciuriware
sciuriware

Depends on what separators you want between the strings.
Or do you want to glue it all together?

String[] from;
String to = "";
     for(int i = 0;  i < from.length;  ++i)
     {
// This is optional:
           if(i > 0)
           {
                 to += "\n";  // or anything else ....
           }
// end optional
           to += from[i];
     }

;JOOP!
You really have two options, one convert the array to a collection and print directly:

i.e.:
Arrays.asList(tmp).toString();

Or alternatively, iterate through the array (in a for loop: for (int i = 0, n = tmp.length; i < n; i++)), and put it together however you like in a StringBuffer
sciuriware --> You will never read element 0 in your example!  You will need to do i++ instead of ++i.  Also, for these sorts of things, always use a StringBuffer!
About StringBuffer (append()) you are right.
About the difference between ++i and i++ .....  GO BACK TO SCHOOL!

Before you say such a thing ... test it first.

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops.  Sorry grim_toaster.  Missed your one-liner there ;-)
--> Before you say such a thing ... test it first

Oops! My bad!  Sorry about that!  It's a for loop isn't it!  Therefore the alteration section will only get called at the end, and seperately!

I do apologise!  (Got distracted whilst finishing off comment).
Just don't teach it.
What ever you post here for an answer will be considered guru output.

AJThomas, don't be confused we're just playing ....

Are you being served well?

;JOOP!
Hey!  Even gurus make mistakes and typos you know! ;)
Yes, don't get me wrong, I only want to point out that the guys (and dolls) who ask are
considered dummies (by some) and those who answer, guru's.
Now, sometimes I answer, but many times I ask, like I see almost all others do.
Only folks like 'objects' never seem to have the need to ask anything .....

What I mean is: when John-in-the-street says that sqrt(2.0) = 1.414213565... nobody believes him untill they check. When Einstein would say 2 + 2 = 7 ..... nobody dares to protest.

Let's get back to business ..

;JOOP!
Part of being a real guru is to be kind & thoughtful in communications. This should hold even for Java gurus. Phrases like 'dolls', 'go back to school','dummies' are not phrases that should be used bu gurus. Gurus will make mistakes, in technical detail and in making thoughtles & politically incorrect comments, they are only human.
;-)
Avatar of AJThomas

ASKER

Many thanks for all the replies.  I don't mind the friendly banter as long as it stays that way.  I look at ithis way.  The more people chipping in the better an answer.  The more people who discuss the given comments - the better the eventual answer, and I get to learn some new stuff as well.

Once again thanks for all you help - I have been very well served ;)
Thanx AJThomas. *Just in case* you didn't know ;-).  It is possible to split points between the people who have commented, if you believe that they have provided useful comments.

Just for future reference ;-)
I don't need the points; I learn from this every time: that's the interest.
Greetings to all of you.
;JOOP!