• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reading from .txt to arraylist?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Greenhorn
Posts: 2
Mac OS X MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be the problem with constructor, nothing to do with the ArrayList I suppose.
 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic