Home > Software engineering >  problems at splitting strings in java
problems at splitting strings in java

Time:10-13

I'm new to java and I want to know how to splits strings with values.

I had an array like this :

String[] CITIES = { 
        "Vancouver 94.0 15.0",
        "Seattle 88.0 52.0",
        "Portland 74.0 88.0",
        "San Francisco 42.0 241.0",
        "Los Angeles 89.0 338.0",
        "Las Vegas 153.0 302.0",
        "Calgary 259.0 15.0",
        "Salt Lake City 224.0 216.0",
        "Phoenix 192.0 348.0",
        "Helena 247.0 90.0"}

and I want to divide each string into 3 values, the name (String) the x-value (double), and the y-value (double)

firstly I thought of cutting at every space, but for the cities like San Francisco, it won't work :( any ideas?

CodePudding user response:

Pattern regex = Pattern.compile("^(.*)  ([0-9.] )  ([0-9.] )$");
for(String city: CITIES) {
    Matcher mx = regex.matcher(city);
    if(mx.find())
        System.out.printf("City: %s, Lat: %s, Lon: %s%n", mx.group(1), mx.group(2), mx.group(3));
}

with output

City: Vancouver, Lat: 94.0, Lon: 15.0
City: Seattle, Lat: 88.0, Lon: 52.0
City: Portland, Lat: 74.0, Lon: 88.0
City: San Francisco, Lat: 42.0, Lon: 241.0
City: Los Angeles, Lat: 89.0, Lon: 338.0
City: Las Vegas, Lat: 153.0, Lon: 302.0
City: Calgary, Lat: 259.0, Lon: 15.0
City: Salt Lake City, Lat: 224.0, Lon: 216.0
City: Phoenix, Lat: 192.0, Lon: 348.0
City: Helena, Lat: 247.0, Lon: 90.0

(note: to convert strings to numbers use e.g. Double::parseDouble)

CodePudding user response:

I suspect this assignment was meant for the OP to learn about the lastIndexOf and substring methods of the String class.

Here are the test results from one of my many tests.

City [name=Vancouver, latitude=94.0, longitude=15.0]
City [name=Seattle, latitude=88.0, longitude=52.0]
City [name=Portland, latitude=74.0, longitude=88.0]
City [name=San Francisco, latitude=42.0, longitude=241.0]
City [name=Los Angeles, latitude=89.0, longitude=338.0]
City [name=Las Vegas, latitude=153.0, longitude=302.0]
City [name=Calgary, latitude=259.0, longitude=15.0]
City [name=Salt Lake City, latitude=224.0, longitude=216.0]
City [name=Phoenix, latitude=192.0, longitude=348.0]
City [name=Helena, latitude=247.0, longitude=90.0]

Here's the complete runnable code. The parseCities method finds the position of the last two spaces and uses substring to "split" the String into three pieces.

public class SplitCities {

    public static void main(String[] args) {
        String[] cities = { 
                "Vancouver 94.0 15.0",
                "Seattle 88.0 52.0",
                "Portland 74.0 88.0",
                "San Francisco 42.0 241.0",
                "Los Angeles 89.0 338.0",
                "Las Vegas 153.0 302.0",
                "Calgary 259.0 15.0",
                "Salt Lake City 224.0 216.0",
                "Phoenix 192.0 348.0",
                "Helena 247.0 90.0" };
        
        SplitCities sc = new SplitCities();
        City[] parsedCities = sc.parseCities(cities);
        
        for (int index = 0; index < parsedCities.length; index  ) {
            System.out.println(parsedCities[index]);
        }
    }
    
    public City[] parseCities(String[] cities) {
        City[] parsedCities = new City[cities.length];
        for (int index = 0; index < cities.length; index  ) {
            String line = cities[index];
            int longitudePosition = line.lastIndexOf(' ');
            int latitudePosition = line.lastIndexOf(' ', longitudePosition - 1);
            String name = line.substring(0, latitudePosition);
            double latitude = Double.valueOf(line.substring(
                    latitudePosition   1, longitudePosition));
            double longitude = Double.valueOf(line.substring(longitudePosition));
            parsedCities[index] = new City(name, latitude, longitude);
        }
        
        return parsedCities;
    }
    
    public class City {
        
        private final double latitude, longitude;
        
        private final String name;

        public City(String name, double latitude, double longitude) {
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public double getLatitude() {
            return latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("City [name=");
            builder.append(name);
            builder.append(", latitude=");
            builder.append(latitude);
            builder.append(", longitude=");
            builder.append(longitude);
            builder.append("]");
            return builder.toString();
        }
        
    }

}

CodePudding user response:

You can use split with a regex that identifies values to split on that starts with a digit " (?=[\\d])"

for (String city : cities) {
    String[] values = city.split(" (?=[\\d])"); 
    System.out.println(Arrays.toString(values));
    // do stuff
}

CodePudding user response:

You could define a regex to parse out the string and two doubles, and split each string using the split method defined here https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

  • Related