Solved

String Array to String Conversion Help

Posted on 2003-11-28
15
763 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:AJThomas
  • 5
  • 4
  • 4
  • +2
15 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 7

Expert Comment

by:grim_toaster
Comment Utility
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
0
 
LVL 7

Expert Comment

by:grim_toaster
Comment Utility
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!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 15

Accepted Solution

by:
jimmack earned 100 total points
Comment Utility
This will copy all the strings in your array into a single string (if this is what you want).

StringBuffer result = new StringBuffer();
for (int i = 0; i < stringArray.length; i++)
{
   result.append(stringArray[i]);
   if (i < stringArray.length)
   {
      result.append(" ");
   }
}

System.out.println(result.toString());

0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Oops.  Sorry grim_toaster.  Missed your one-liner there ;-)
0
 
LVL 7

Expert Comment

by:grim_toaster
Comment Utility
--> 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).
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 7

Expert Comment

by:grim_toaster
Comment Utility
Hey!  Even gurus make mistakes and typos you know! ;)
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 1

Expert Comment

by:mal4mac
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
;-)
0
 
LVL 4

Author Comment

by:AJThomas
Comment Utility
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 ;)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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 ;-)
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
I don't need the points; I learn from this every time: that's the interest.
Greetings to all of you.
;JOOP!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now