Dear all,

I have following code and i want to write elements of array in single string(merge with ,) , in between all the values.
like
a[0] = "hi"
a[1] = "hello"
a[2] = 1

a[0] = "get"
a[1] = "post"
a[2] = 2
a[3] = 45

then i want output like
String str;
str = hi,hello,1
str = get,post,2,45

and so on.

I have do so far is.I am taking value from hashmap having name temp.

for(Map.Entry<Integer, String[]> mapEntry2 : temp.entrySet())
                {
                    int key3 = mapEntry2.getKey();
                    //System.out.print("\nvalue of key3 = "+key3);
                    String[] aa5 = (String[]) temp.get(key3);
                    for(int g=0;g<aa5.length;g++)
                    {

                    }

                }

So what i have to write at inside for loop to create and store it with .csv file
each key contains all elements of one line.

Thanks

Define a string somewhere and call it whatever... i called it str...then inside that loop use this:

// for the last word, no comma afterwards...
if (i == (aa5.length - 1)) {
    str = str + aa5[i];
} else {
    str = str + aa5[i] + ", ";
}

You could use a String or StringBuffer class. If you use String, it would be simplier to check whether the incoming array is null or empty. If it is, return an empty string. Otherwise, initialise the value with the first element of the array. Then go through a loop starting from index 1. Add each index element with your delimiter and the string.

// i.e.
if array is null or array is empty
  return empty string
else
  str <- array[0]
  for each index starting from 1 up to array size *inclusive*
    concatenate str with ", " and array[index]  // the ", " can be any delimiter
  end for
  return str
end if

@Taywin: I think you meant your loop to be :
for (int i = 1; i < array[index]; i++

and NOT
for (int i = 1; i <= array[index]; i++)

Why inclusive? Pseudo code syntax?

@NP-complete, the word inclusive in pseudo code means you do not include the value in your range. What I said means [1, array_size). If it is exclusive, it means the value will be included in the range.

@NP-complete, the word inclusive in pseudo code means you do not include the value in your range.

Actually, it's the other way around. Exclusive means that an end of a range is not included and inclusive means that an end of a range is included. The square bracket means inclusive and the paren means exclusive, so:

[a,b] - Fully inclusive, closed
(a,b) - Fully exclusive, open
[a,b) - Half open, excludes b
(a,b] - Half open, excludes a

Or in other terms, [a] represents the set {a} while (a) represents an empty set {}.

What I said means [1, array_size).

What you said means [1,array_size], which is congruent with the usual pseudocode interpretation. If what you meant was a half open range then it would be closer to:

for each index starting from 1 up to array size - 1

If it is exclusive, it means the value will be included in the range.

I find your explanation hilarous given that include and exclude are opposites. ;)

OH? All the time I misunderstood the words? >_<

All the time I misunderstood the words? >_<

Seems like it. Better here than in a professional setting though, right? That's happened to me an it's mighty embarrassing.

Thanks to all.
I got it.And problem is solved.

Again thanks all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.