0

My input string is of the format

12.8478746,77.6632938,5:20:38 PM
12.8478746,77.6632938,5:20:49 PM
12.8478746,77.6632938,5:40:05 PM
................................
12.8478746,77.6632938,5:40:14 PM

Number of rows is unknown. I need to parse the string and store it like (double lat,double long,string time). Also, I need to make a function call, with (lat,long,time) as arguments, 'n' times where n depends on number of rows of input string . How do I do this.

This is what I tried:

    String[] lines = result.split("\\s+"); // split on new lines

    for (int i = 0; i < lines.length; i++) {
        String[] temp = lines[i].split("\\s*,\\s*");
            double lat = Double.parseDouble(temp[0]);
            double lo = Double.parseDouble(temp[1]);
            AddMarker(lat,lo,temp[2]);
    }

This wont work because in "5:20:38 PM" there is a space between 5:20:38 and PM and in my input each row is separated by a space. So, I'm getting the error: Invalid double "PM"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
mami
  • 41
  • 4
  • 1
    Solve the issue, or write code for you? Because we would like to see what you tried already – OneCricketeer Apr 09 '17 at 15:17
  • You could solve it easy using a split, or even a regex, i guess a regex would look awesome =). i was gonna solve it for you, but yeah i agree with cricket_007, what have you done ? – Yussef Apr 09 '17 at 15:24
  • You can use a simple regex to split the input string. For storing you can create a class with properties lat, long and time and maintain a list or similar of it. – Hardik Modha Apr 09 '17 at 15:25
  • 1
    Note that `(lat, long,time)` is not a pair, it's a *triple*. – RealSkeptic Apr 09 '17 at 15:40
  • @cricket_007 I've edited my question to show what I tried – mami Apr 09 '17 at 16:07
  • Possible duplicate of [CSV API for Java](http://stackoverflow.com/questions/101100/csv-api-for-java) – OneCricketeer Apr 09 '17 at 18:42

2 Answers2

0

What you need to do is to work differently with split, it should accept the the separator character, don't treat it as a regular expression:

String[] lines = result.split("\n");  // split on new lines

Or possibly:

String[] lines = result.split("\r\n");

And also:

String[] temp = lines[i].split(",");

If you want to store the date & time ("5:20:38 PM") as string, then just pass temp[2] as you already do. If you want to parse as DateTime or something like that, see this link.

Community
  • 1
  • 1
Ossin Java guy
  • 365
  • 3
  • 12
0

You should use a regex. Example :

    final String regex = "(.*),(.*),(.*)";
    final String string = "12.8478746,77.6632938,5:20:38 PM";

    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(string);

    matcher.find();

    double lat = Double.valueOf(matcher.group(1));
    double lo = Double.valueOf(matcher.group(2));
    String time = matcher.group(3);

    System.out.println("LAT: "+lat);
    System.out.println("LO: "+lo);
    System.out.println("TIME: "+time);

Result :

LAT: 12.8478746
LO: 77.6632938
TIME: 5:20:38 PM
cactuschibre
  • 1,908
  • 2
  • 18
  • 36