This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on assignment  RSS feed

 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Entrant class which I am not allowed to change which is below



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.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know just looking at the code. But my last line might help.
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
System.out.println(line);
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?
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . Not the way I would have done it, however. . . .
We do see some assignments in a style we would never use ourselves.
 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you're starting from



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.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . write a method whose signature looks like this:. . .
What a good idea. But wouldn't such a factory method would be public static, in an EntrantFactory utility class?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!