Skip to content
Advertisement

problems at splitting strings in java

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?

Advertisement

Answer

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();
        }
        
    }

}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement