• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from .txt to arraylist?  RSS feed

 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I am currently working on a program that will read from a .txt file to create an arraylist. I will be working with the objects in the list to perform computations and display the data. The .txt file is in the following format:

lastName firstName hoursWorked hourlyRate

With no delimiters. I've messed with this thing way too much in the last 12 hours (only day off for the week ) so variable names etc have changed often. If there are errors in the program due to that I profusely apologize! Any help would be greatly appreciated.

At present the error I am receiving is "error: constructor GrossPayService in class GrossPayService cannot be applied to given types". I understand what the error means, but not how to fix it. There may be other errors as well, but at present that is all my compiler is giving me.



 
Varun Chandra Jammula
Greenhorn
Posts: 2
Mac OS X MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be the problem with constructor, nothing to do with the ArrayList I suppose.
 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What trouble are you having in deciding how to change your GrossPayService constructor declaration(s) or the arguments to the constructor on line 21 to fix this error?

As an aside, an addEmployee that takes arguments of the employee to add makes sense, but seeing it read one or more employees from a file doesn't. I would do that in a readEmployees(String fileName) or some such thing.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks like a class written specifically to sort out the problem reading from the file. That is a good idea. Agree that the error message suggests you are passing the wrong types for the constructor. Please show us the constructor for the gross pay class.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has somebody told you they want an LF character? If not, avoid \n. Use printf and %n instead.
Also try to reduce the number of print calls. You will get a slight performance enhancement if you can join two print calls into one.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Anawalt wrote:As an aside, an addEmployee that takes arguments of the employee to add makes sense, but seeing it read one or more employees from a file doesn't. I would do that in a readEmployees(String fileName) or some such thing.

Totally agree.

@Cody: Another point:
You don't need to enclose code in a try...catch block just because it throws a checked Exception.

Your block doesn't add any value at all, because all it does is swallow an Exception and exit your program with a message that is probably far less informative than the original stacktrace would be. Also: you don't close() the FileReader.

My advice - either:
1. Use a try-with-resources block, and re-throw any IOException as an IOError, containing the original Exception as a 'cause'.
or:
2. Add 'throws IOException' to your addEmployee() method declaration, and remove the try...catch block altogether.

Winston
 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the welcomes and help everyone!

I do not have a constructor - everything I have posted so far is what I've got, at this point. Do I need one? What should go in it? I have used them before, but this is my first time using arrayList.

I have tried added 'throws IOException' and removing try...catch. Now my code looks like this:



and I am receiving an error message stating that

required: String,double,double
found: String,String,double,double
reason: actual and formal argument lists differ in length

when trying to create an object.
 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is GrossPayService defined? It sounds like it has a constructor that takes a string and two doubles. Let's see it.
 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said, all the code I have is posted. That's why I'm confused that it says it is defined as having three variables - I can't find anywhere in the code where I stated that.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That can't possibly be all the code, there must also be a class called GrossPayService as Jacob has pointed out (It may be the class was supplied by your teacher rather than you writing it). Also please post the full compiler message as it gives us more information on where the problem is occurring than the snippet you have supplied.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!