-4

I have a String with value as:

String lstr = [12.88, 77.56],[12.81, 77.7156]

....so on

I need to parse and iterate it and somehow substitute the values as :

final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
        points.add(new Coordinate(12.88, 77.56));
        points.add(new Coordinate(12.81, 77.7156));

i tried converting string to List and then iterating it using for loop, but it is not working, either it goes out of bound or extra square bracket throws an exception. What is the best way to parse, format and iterate a string like this?

user2093576
  • 3,082
  • 7
  • 32
  • 43

1 Answers1

2

You can do something like in the example below. Note you can improve your regex to check for spaces etc.:

String lstr = "[12.88, 77.56],[12.81, 77.7156]";

List<Coordinate> cors = new ArrayList<Coordinate>();

String []coordinates = lstr.split("\\],\\[");

for(String cordinate:coordinates)
{
    String []xy = cordinate.split(",");
    cors.add(new Coordinate(xy[0],xy[1]));
}
System.out.println(cors);
Stepan Novikov
  • 1,402
  • 12
  • 22