1

Some of the file I'm working with: http://pastebin.com/WriQcuPs

Currently I had to make the population, latitude, and longitude strings or else I wouldn't get the desired output. I want for them to be int, double, and double in that order.

public class City {
    String countrycode;
    String city;
    String region;
    String population;
    String latitude;
    String longitude;

    public City (String countrycode, String city, String region, String population, String latitude, String longitude) {
        this.countrycode = countrycode;
        this.city = city; 
        this.region = region;
        this.population = population;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String toString() {
        return this.city + "," + this.population 
                + "," + this.latitude + ","
                + this.longitude; 
    }
}

I suspect it has something to do with how I created the array list. Is there a way to make it so that some of the elements of the list are of a different type? I tried changing it to ArrayList<City> and changing the data types in the City class but it still gave me a ton of errors.

public class Reader {

    In input = new In("file:world_cities.txt");
    private static City cityInfo;

    public static void main(String[] args) {
        // open file
        In input = new In("world_cities.txt");
        input = new In("world_cities.txt");
        try {
        // write output to file
        FileWriter fw = new FileWriter("cities_out.txt");
        PrintWriter pw = new PrintWriter(fw);

        int line = 0;

        // iterate through all lines in the file
        while (line < 47913) {

            // read line
            String cityLine = input.readLine();

            // create array list
            ArrayList<String> cityList = new ArrayList<String>(Arrays.asList(cityLine.split(",")));

            // add line to array list
            cityList.add(cityLine);

            // increase counter
            line += 1;      

            // create instance
            cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), cityList.get(4), cityList.get(5));
            System.out.println(cityInfo);

            // print output to file 
            pw.println(cityInfo); 
        }

        // close file
        pw.close();
        }

        // what is printed when there is an error when saving to file
        catch (Exception e) {
            System.out.println("ERROR!");
        }

        // close the file
        input.close();
    }
}

7 Answers 7

1

If you declare the list as follows, you can put instances of any reference type into it:

    List<Object> list = new ArrayList<>();

But the downside is that when you get an element from the list, the static type of the element will be Object, and you will need to type cast it to the type that you need.

Also note, that you can't put an int or a double into a List. Primitive types are not reference types, and the List API requires the elements to be instances of reference types. You need to use the corresponding wrapper types; i.e. Integer and Double.


Looking at more of your code, I spotted this:

ArrayList<String> cityList = 
    new ArrayList<String>(Arrays.asList(cityLine.split(",")));

If you change the list to List<Object> where the objects are either Integer or Double, you won't be able to build your list like that.

In fact, the more I look this, the more I think that you don't need a list at all. You should be doing something like this:

    // read line
    String cityLine = input.readLine();

    String[] parts = cityLine.split(",");
    // increase counter
    line += 1;      

    // create instance
    cityInfo = new City(parts[0], parts[1], parts[2], 
                        Integer.parseInt(parts[3]),
                        Double.parseDouble(parts[4]),
                        Double.parseDouble(parts[5]));

Notice: there is no List there at all!!

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

1 Comment

Awesome! Works great. Thank you so much!
0

You could parse the string values before they are passed into a new City object. Then you could change the constructor and variables within a City to be an int, double, and double.

int pop = Integer.parseInt(cityList.get(3));
double latitude = Double.parseDouble(cityList.get(4));
double longitude = Double.parseDouble(cityList.get(5));
cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), pop, latitude, longitude);

Comments

0

By looking at the City class you have defined the members are of different primitive data types. When you read the file to create a City, you need to pass the constructor parameters with the defined data types as in your constructor.

Modify your City class as below :

public class City {
    String countrycode;
    String city;
    String region;
    int population;
    double latitude;
    double longitude;
...

Try the following :

cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(cityList.get(5)));

This will convert the Strings to int and double as desired by the City class.

Comments

0

I believe no, but you can do this

public class City {
String countrycode;
String city;
String region;
String population;
double latitude;
double longitude;

public City (String countrycode, String city, String region, String population, double latitude, double longitude) {
    this.countrycode = countrycode;
    this.city = city; 
    this.region = region;
    this.population = population;
    this.latitude = latitude;
    this.longitude = longitude;
}

public String toString() {
    return this.city + "," + this.population 
            + "," + this.latitude + ","
            + this.longitude; 
}
}

public class Reader {

In input = new In("file:world_cities.txt");
private static City cityInfo;

public static void main(String[] args) {
    // open file
    In input = new In("world_cities.txt");
    input = new In("world_cities.txt");
    try {
    // write output to file
    FileWriter fw = new FileWriter("cities_out.txt");
    PrintWriter pw = new PrintWriter(fw);

    int line = 0;

    // iterate through all lines in the file
    while (line < 47913) {

        // read line
        String cityLine = input.readLine();

        // create array list
        ArrayList<String> cityList = new ArrayList<String>(Arrays.asList(cityLine.split(",")));

        // add line to array list
        cityList.add(cityLine);

        // increase counter
        line += 1;      

        // create instance
        cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), cityList.get(3), Doule.parseDouble(cityList.get(4)), Doule.parseDouble(cityList.get(5)));
        System.out.println(cityInfo);

        // print output to file 
        pw.println(cityInfo); 
    }

    // close file
    pw.close();
    }

    // what is printed when there is an error when saving to file
    catch (Exception e) {
        System.out.println("ERROR!");
    }

    // close the file
    input.close();
}
}

Comments

0

You can do something like this:

READER CLASS

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class Reader {

    In input = new In("file:world_cities.txt");
    private static City cityInfo;

    public static void main(String[] args) {
        // open file
//        In input = new In("world_cities.txt");
//        input = new In("world_cities.txt");
        try {
        // write output to file
        FileWriter fw = new FileWriter("cities_out.txt");
        PrintWriter pw = new PrintWriter(fw);

        int line = 0;

        // iterate through all lines in the file
        while (line < 47913) {

            // read line
            String cityLine = input.readLine();

            // create array list
            ArrayList<String> cityList = new ArrayList<String>(Arrays.asList(cityLine.split(",")));

            // add line to array list
            cityList.add(cityLine);

            // increase counter
            line += 1;      

            // create instance
            cityInfo = new City(cityList.get(0), cityList.get(1), cityList.get(2), Integer.parseInt(cityList.get(3)), Double.parseDouble(cityList.get(4)), Double.parseDouble(cityList.get(5)));
            System.out.println(cityInfo);

            // print output to file 
            pw.println(cityInfo); 
        }

        // close file
        pw.close();
        }

        // what is printed when there is an error when saving to file
        catch (Exception e) {
            System.out.println("ERROR!");
        }

        // close the file
        input.close();
    }
}

CITY CLASS

public class City {
    String countrycode;
    String city;
    String region;
    int population;
    double latitude;
    double longitude;

    public City (String countrycode, String city, String region, int population, double latitude, double longitude) {
        this.countrycode = countrycode;
        this.city = city; 
        this.region = region;
        this.population = population;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String toString() {
        return this.city + "," + this.population 
                + "," + this.latitude + ","
                + this.longitude; 
    }
}

Comments

0

Is there a way to make it so that some of the elements of the list are of a different type?

It is possible to have a List with elements of different type, but not if you're populating it with String.split() -- because that returns a String[].

You can convert the strings you get back fro String.split() into the desired types with the valueOf methods on the Java primitive type wrappers, for example...

Integer population = Integer.valueOf("1234567");  // Returns 1234567 as an Integer, which can autobox to an int if you prefer

Comments

0

Like Stephen suggested, you can use List<Object>, besides that, you can just pass String to City but let City itself to handle the datatype.

public class City {
    String countrycode;
    String city;
    String region;
    int population;
    double latitude;
    double longitude;

    public City (String countrycode, String city, String region, String population, String latitude, String longitude) {
        this.countrycode = countrycode;
        this.city = city; 
        this.region = region;
        try{
            this.population = Integer.parseInt(population);
            this.latitude = Double.parseDouble(latitude);
            this.longitude = Double.parseDouble(longitude);
        }catch(Exception e){
            e.printStacktrace();
        }

    }

    public String toString() {
        return this.city + "," + this.population 
                + "," + this.latitude + ","
                + this.longitude; 
    }
}

Btw, you have initiated In twice.

In input = new In("world_cities.txt");
input = new In("world_cities.txt");

Modify it to

In input = new In("world_cities.txt");

1 Comment

Whoops! Good catch!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.