I then have to create a class called WCCR, create a private instance variable called entrants to reference a list of Entrant objects in a rally and add a line of code to the constructor to assign a new instance of ArrayList to entrants. After that, I have to create a method called readInEntrants() which takes no arguments and returns no values and it must read from the file entrants.txt. The text file has has the entrant's name and car category in CSV format. For example, John Smith,E1. The method should:
prompt the user to select an appropriate text file
- Promt the user to select an apprpriate file
- Make use of BufferedReader and Scanner objects to read the file line by line
- As each line (which contains the name of an entrant and their category of car) is read, a new Entrant object should be created and its instance variables set using the values found in the line. That instance of Entrant should be added to the list referenced by the instance variable entrants.
It complies and I select the correct file, but the the name and category of each object is just an empty string. When I try to add things to the list referenced by the instance variable entrants I get the following error "constructor Entrant in class Entrant cannot be applied to given types: required: no arguments; found:java.lang.String, java.lang.String; reason: actual and formal argument lists differ in length".
Been stuck on it for a couple of days and have no idea what I am doing wrong.
I shall make a suggestion later, but before that, stop using finally to close the buffered reader. Use try with resources instead. It will save you about 8 lines of code. There is also no need to initialise the reader to null.
I think I should introduce you to some rather strange syntax:-It looks strange, but it is a common way to read the lines with a buffered reader. You can rely on the loop stopping when you reach the end of the file, but correct placement of the () is essential.
I would suggest you start by adding
or similar to the contents of the loop, so as to verify that you are actually reading from the file at all.
Why are you using a Scanner as well as a buffered reader? You can do all that reading directly with a Scanner.
Why are you adding a new Entrant to the List and not setting any of its fields?
Carey Brown wrote:The signature for the Entrant constructor is
As you can see, it takes no arguments. (Not the way I would have done it, however.)
That only leaves you the choice of using this constructor, followed by some number of setter calls to fill in the members.
That is what I originally thought to do, but I couldn't figure out how to do it and I am still stuck. I keep getting error after error. I know it is simply which makes it more frustrating lol and I start to over think it even more
and trying to tweak that to use the actual constructor, then it's not surprising you're getting nowhere. Here's the steps you need to do:
1. Create an Entrant object
2. Set the Entrant's name
3. Set the Entrant's category
4. Add the Entrant to the list
It's not a one-liner any more but it's a straightforward way of getting the job done.
Or if you really like one-liners you could write a method whose signature looks like this:
and use that method.