Skip to content
Advertisement

Trying to store varibles from a txt file

I’m trying to read the file and store each variable from the file to its correct spot i want to save from file as Firstname, Lastname , DollarPerHour. Then after that print out each person separately along with a equation to solve their check for the week but I have no idea how to store then call correct from each separate person

JavaScript

JavaScript

my txt file is set up Like

JavaScript

Advertisement

Answer

If all your lines are formatted as [firstName],[lastName],[hoursWorked] (I assume your numeric value is your hours worked and not the “dollars per hour”) for instance, you could simply use the split method over your line object (weirdly called file) :

JavaScript

Now, you have an array of strings like this: [“Michael”, “Zargosa”, “14”] You still need to convert your “14” string into an int, which can be done this way :

Integer.parseInt(…)

Eventually, you could write a method turning your line into a Worker object :

JavaScript

A more elegant and safer way to do this, would probably be consist in a regular expression based data extraction. The minimal implementation could involve checking that your line matches this regex: w+,w+,d+


Your class “Main” should not inherit (extend) Workers class since it doesn’t make sense. I don’t really know what you are trying to do, but if you are willing your main method to use or “contain” (instances of) the Workers class, there is no need for the class to inherit from Workers.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement