【问题标题】:ArrayList adds null instead of StringArrayList 添加 null 而不是 String
【发布时间】:2020-04-13 10:13:44
【问题描述】:

我正在尝试创建一个程序,该程序处理 ArrayList 中的多个城市,然后将它们保存在 txt 文件中,并从同一文件中加载数据(如果之前已创建)。问题是当我对 ArrayList 中的数据进行硬编码时,前两个字符串(名称和国家/地区)为空,而其他所有字符串都很好。感谢您的时间。 代码如下:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.io.*;

public class Start {

    public static void main(String[] args) throws IOException {
        City test = new City("Paris","France",0,0,"Sunny",0,123.123,123.123);
        City test2 = new City("Tokyo","Japan",0,0,"Sunny",0,123.123,123.123);
        ArrayList<City> list=new ArrayList<City>();
        list.add(test);
        list.add(test2);
        File file = new File("test.txt");
        if(file.exists()) {
            try{
                load(file, list);
            }catch(IOException e) {
                System.out.println("There was an error loading the file");
                System.exit(0);
            }
            System.out.println("Data have been found in the machine and have been loaded succesfully");
        }
        else {
            System.out.println("No data have been found on the machine");
        }
        City obj;
        ListIterator<City> i = list.listIterator();
        while(i.hasNext()) {
            obj = i.next();
            System.out.println(obj.getName()+" "+obj.getCountry());
        }
        save(file,list);


    }

    static void load(File fl, List<City> ct) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader(fl));
        String str;
        String[] objs = new String[8];
        City city = new City("nothing", "nothing", 0, 0, "nothing", 0, 0, 0);
        while((str=bf.readLine()) != null) {
            objs = str.split(" ");
            city.setName(objs[0]);
            city.setCountry(objs[1]);
            city.setCafeRestaurants(Integer.valueOf(objs[2]));
            city.setLatitude(Double.valueOf(objs[3]));
            city.setLongitude(Double.valueOf(objs[4]));
            city.setMuseums(Integer.valueOf(objs[5]));
            city.setParks(Integer.valueOf(objs[6]));
            city.setWeather(objs[7]);
            ct.add(city);
        }
        bf.close();
    }

    static void save(File fl, List<City> users) throws IOException {
        FileWriter fw = new FileWriter(fl);
        City obj;
        ListIterator<City> i = users.listIterator();
        while(i.hasNext()) {
            obj= i.next();
            fw.write(obj.getName()+" "+obj.getCountry()+" "+obj.getCafeRestaurants()+" "+obj.getLatitude()+" "+obj.getLongitude()+" "+obj.getMuseums()+" "+obj.getParks()+" "+obj.getWeather()+"\n");

        }
        fw.close();
    }

} 

城市类:

public class City {
    String name;
    String country;
    int museums;
    int cafeRestaurants;
    String weather;
    int parks;
    double latitude;
    double longitude;

    public City(String name,String Country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude) {
        this.museums = museums;
        this.cafeRestaurants = cafeRestaurants;
        this.weather = weather;
        this.parks = parks;
        this.latitude = latitude;
        this.longitude = longitude;
    }


    public City(int museums, int cafeRestaurants, String weather, int parks) {
        super();
        this.museums = museums;
        this.cafeRestaurants = cafeRestaurants;
        this.weather = weather;
        this.parks = parks;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getCountry() {
        return country;
    }


    public void setCountry(String country) {
        this.country = country;
    }
    public int getMuseums() {
        return museums;
    }

    public void setMuseums(int museums) {
        this.museums = museums;
    }

    public int getCafeRestaurants() {
        return cafeRestaurants;
    }

    public void setCafeRestaurants(int cafeRestaurants) {
        this.cafeRestaurants = cafeRestaurants;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public int getParks() {
        return parks;
    }

    public void setParks(int parks) {
        this.parks = parks;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    } 

    public void setDefault() {
        this.setCafeRestaurants(0);
        this.setMuseums(0);
        this.setParks(0);
        this.setWeather("Sunny");
    }

}

【问题讨论】:

  • 您没有在构造函数中分配namecountry

标签: java arraylist io null


【解决方案1】:

您错过了初始化和分配 name 和 country 变量的值。更新了 City Class 的构造函数供您参考,

public City(String name, String Country, int museums, int cafeRestaurants, String weather, int parks,
        double latitude, double longitude) {
    this.name = name;
    this.country = Country;
    this.museums = museums;
    this.cafeRestaurants = cafeRestaurants;
    this.weather = weather;
    this.parks = parks;
    this.latitude = latitude;
    this.longitude = longitude;
}

【讨论】:

    【解决方案2】:

    您忘记在构造函数中对 name 和 country 变量进行初始化和赋值。

    【讨论】:

      【解决方案3】:

      City 类中的构造函数City(String name,String Country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude),您没有将nameCountry 的值分配给局部变量。

      public City(String name,String Country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude) {
              this.museums = museums;
              this.cafeRestaurants = cafeRestaurants;
              this.weather = weather;
              this.parks = parks;
              this.latitude = latitude;
              this.longitude = longitude;
          }
      

      将上面的代码替换为:

      public City(String name,String Country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude) {
              this.name =name;
              this.country = Country;
              this.museums = museums;
              this.cafeRestaurants = cafeRestaurants;
              this.weather = weather;
              this.parks = parks;
              this.latitude = latitude;
              this.longitude = longitude;
          }
      

      提示:使用正确的命名约定,例如 String Country -> String country

      (参考:https://www.oracle.com/technetwork/java/codeconventions-135099.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多