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
public class Workers { private String FirstName; private String LastNAme; private int HoursWorked; private int DollarPerHour; public String getFirstName() { return FirstName; } public void setFirstName(String firstName) { FirstName = firstName; } public String getLastNAme() { return LastNAme; } public void setLastNAme(String lastNAme) { LastNAme = lastNAme; } public int getHoursWorked() { return HoursWorked; } public void setHoursWorked(int hoursWorked) { HoursWorked = hoursWorked; } public int getDollarPerHour() { return DollarPerHour; } public void setDollarPerHour(int dollarPerHour) { DollarPerHour = dollarPerHour; } @Override public String toString() { return "Workers{" + FirstName + LastNAme + ", Has Worked " + HoursWorked + "hours this week"+ "and makes " + DollarPerHour + "a hour"; } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Main extends Workers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try{ BufferedReader reader = new BufferedReader(new FileReader("src/WorkersAndPay.txt")); String file = reader.readLine(); while (file != null){ System.out.println(file); file = reader.readLine(); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
my txt file is set up Like
Michael,Zargosa,14 MAx,SMith,15 Judith,Smith,16
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) :
String[] data = file.split(",");
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 :
private static Workers createFromLine(final String line) { Workers worker = null; if(line != null) { worker = new Workers(); String[] data = line.split(","); int workedHours = Integer.parseInt(data[2]); worker.setFirstName(data[0]); worker.setLastNAme(data[1]); worker.setHoursWorked(workedHours); } return worker; }
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.