0

Disclaimer : I'm using this Post, as reference for List<Object> to List<String> and this Post for Java List<String> of strings to a JavaScript array.

I've List<Seat> and I want to get all values of it in a comma separated String, I tried in this way

import java.util.*;
import java.lang.*;

class Rextester
{  

    public Rextester(){
        Seat seat1 = new Seat();
        seat1.setSeatNumber(1);

        Seat seat2 = new Seat();
        seat2.setSeatNumber(2);

        Seat seat3 = new Seat();
        seat3.setSeatNumber(3);


        List<Seat> seatList = new ArrayList<Seat>();
        seatList.add(seat1);
        seatList.add(seat2);
        seatList.add(seat3);
        Utility util = new Utility();
        String stringSeats = util.toJavascriptArray(seatList);
        System.out.println("JavaScriptArray is " + stringSeats);
    }

    public static void main(String args[])
    {
        new Rextester();

    }



    private class Seat {

        private Integer seatNumber;        

        public Integer getSeatNumber() {
            return this.seatNumber;
        }

        public void setSeatNumber(Integer seatNumber) {
            this.seatNumber = seatNumber;
        }   
        public String toString() {
            return ""+ seatNumber;
        }
    }

    private class Utility {

        public String toJavascriptArray(List<Seat> listSeats){
            List<String> strings = new ArrayList<String>();
            for (Seat object : listSeats) {
                strings.add(object != null ? object.toString() : null);
            }
            String[] arr = new String[strings.size()];
            arr = strings.toArray(arr);
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            for(int i=0; i<arr.length; i++){

                if(i+1 < arr.length){
                    sb.append(",");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }
}

but this gives me

JavaScriptArray is [,,]

on console, am I making some mistakes? an online working code is http://rextester.com/NDUGT61105

2
  • Everything is right.. besides the fact in the toJavascriptArray(List<Seat> listSeats) method... You aren't appending the string in there... just a comma Commented Jun 11, 2016 at 4:28
  • So add this in there... sb.append(arr[i]); Commented Jun 11, 2016 at 4:30

3 Answers 3

2

You didn't append iterated element, see below

        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]); // add this
            if (i + 1 < arr.length) {
                sb.append(",");
            }
        }

have a look at the toString implementations of Arrays and List

Simply you can return

strings.toString()

or

Arrays.toString(arr)

To get the expected result

Sign up to request clarification or add additional context in comments.

1 Comment

Ok that is the point I was missing added issue resolved.
1

Another option, if you're using Java 8 is using StringJoiner:

https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html

1 Comment

Thanks for your time and Sorry, but I'm using java 7.
0

You forgot to append the array elements to the string buffer! Add

sb.append(arr[i]);

inside the loop where you prepare the string buffer and everything is ok

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.