简体   繁体   中英

Cutting 2 numbers off a string

I'm reading a .txt file with a list of locations and coordinates.

Each line contains the name of a location, its latitude and longitude:

Downtown Flaggington 49.46419 -81.995353
Bender and Soonr 40.45519 -88.909353
...

How can I chop the last 2 numbers off of each line to just have the name?

This is what I have:

public class GPSFileReverser
{
    public static String reverse(Scanner in, String name, double lat, double lon) throws FileNotFoundException{

        in = new Scanner(new File("gps-and-place.txt"));

        GPSLocation[] gps = new GPSLocation[100];

        for(int i = 0; i < 100; i++) {
            name = in.nextLine();
            name= name.;
            lat = in.nextDouble();
            lon = in.nextDouble();
            GPSLocation currentGPS = new GPSLocation(name, lat, lon);
            gps[i] = currentGPS;


        }
        return name + " " + lat + " \t " + lon;
    }
}

using regular expressions

        String s = "Bender and Soonr 40.45519 -88.909353";
        String pt = "^(.*) (-?\\d*\\.\\d*) (-?\\d*\\.\\d*)$";
        Pattern p = Pattern.compile(pt);
        Matcher m = p.matcher(s);
        if(m.find())
        System.out.println(m.group(1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM