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"

6
  • 1
    Solve the issue, or write code for you? Because we would like to see what you tried already Commented Apr 9, 2017 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 ? Commented Apr 9, 2017 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. Commented Apr 9, 2017 at 15:25
  • 1
    Note that (lat, long,time) is not a pair, it's a triple. Commented Apr 9, 2017 at 15:40
  • @cricket_007 I've edited my question to show what I tried Commented Apr 9, 2017 at 16:07

2 Answers 2

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.

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

1 Comment

Personally, I'd use a real CSV parser. Splitting on raw commas isn't always the best idea
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

2 Comments

CSV records don't need a regex
Yes, if it is a CSV file to parse, use a real CSV parser like Apache Common CSV (commons.apache.org/proper/commons-csv/archives/1.4/apidocs/…).

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.