Home > Back-end >  Trying to store varibles from a txt file
Trying to store varibles from a txt file

Time:10-02

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

CodePudding user response:

   public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File("./WorkersAndPay.txt")));
            while (br.ready()) {
                String line = br.readLine();
                if (line == null){continue;}
                String[] words = line.split(",");
                Workers worker = new Workers();
                worker.setFirstName(words[0]);
                worker.setLastName(words[1]);
                worker.setHoursWorked(Integer.parseInt(words[2]));
                System.out.println(worker.toString());
            }
            br.close();
        } 
        catch (IOException e) {e.printStackTrace();}
    }

CodePudding user response:

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.

CodePudding user response:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Main  {


    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){
                file = reader.readLine();
                String[] data = file.split(",");
                Workers worker = new Workers();
                worker.setFirstName(data[0]);
                worker.setLastName(data[1]);
                worker.setHoursWorked(Integer.parseInt(data[2]));
                worker.setDollarPerHour(Integer.parseInt(data[3]));
                System.out.println(worker.toString());


            }

            reader.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//I did this and it prints out also change my txt to be see up like 
//firstname,lastname,hourswroked,dallorPerhour
 /* change to string to 
@Override
public String toString() {
    return "Workers{"  
            firstName   " "  lastName  
            ", Has Worked "   hoursWorked   " hours this week Check will be: " 
            (dollarPerHour * hoursWorked)   " $ ";

}

*/



/*
Workers{Michael Murillo, Has Worked 50 hours this week Check will be: 550 $ 
Workers{Max Murillo, Has Worked 49 hours this week Check will be: 637 $ 
Workers{Eddie Murillo, Has Worked 50 hours this week Check will be: 700 $ 
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.split(String)" because "file" is null
    at Main.main(Main.java:19)

*/

  • Related