#Lost on how to approach writing an overloaded constructor taking a String as an argument

1 messages · Page 1 of 1 (latest)

graceful lark
#

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.

fierce sinewBOT
#

<@&987246399047479336> please have a look, thanks.

lyric moon
#

The constructor seems alright, the one with String param

#

Opening files is a risky business, it can cause problems that's why gpt suggested something with try-catch block.

covert grove
#

you need to look into how to read files in Java. with the Files utility class, you can avoid try/catch by using something such as Files.readAllLines

edit: nevermind, it throws an IOException

lyric moon
graceful lark
lyric moon
# graceful lark can you explain how try-catch works? I might just integrate that into my solutio...

Sure, when you are iffy about something that may or may not break. You write that inside try block, and catch block contains the stuff to do incase of said failure.

For ex incase of opening file, it's something not in your hand. Whoever uses your overloaded constructor will pass file name to it, that file name can be correct or not. That's when you get IOException, so try the risky part inside try block(opening file) and catch IO exception inside catch block and then you what do you want to do knowing user passed a file name that does not exist? You can inform the user about this mistake.

fierce sinewBOT
#

@graceful lark

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

graceful lark
lyric moon