I'm learning java right now, and lately I've been having trouble with how to encounter this problem. I'm trying to write an overloaded constructor taking a filename as type string as an argument. In the class, I'm supposed to instantiate a new ArrayList of a type defined in a different class I created (City), then store it in the current class's instance method. After that the constructor is supposed to open and read the filename named as the argument, and then for each line in the file, instantiate a new object of type City with the name, state, highTemp, and lowTemp (all instance variables in my City class) on that line, and add this City to the ArrayList cityList.
I asked ChatGPT to help me, and it did provide me code that looked like it worked, but it used try-catch blocks and other terminology I am not quite familiar with in my current state. I would like to know how to approach the problem without using those different blocks.
This is my code so far:
import java.util.;
import java.io.;
public class ClimateZone {
private ArrayList<City> cityList;
public ClimateZone() {
this.cityList = new ArrayList<City>();
}
public ClimateZone(String cityFileName) {
this.cityList = new ArrayList<City>();
}
}
If additional information, such as the code within my City class, is needed, please let me know and I can provide it. Thanks in advance.