1

So I am working on a project to get data from a text file to calculate things like payment and tax. This is what I have so far and don't know where to go from here. I can't even open the text file for the data. Any help would be appreciated!

newdata.txt
id; name; position; hourly rate; hours worked

That is the text file containing the input data ^.


The output should be something like this:

Output:

Employee ID: 1

Employee Name: ab

Position: c

Gross Pay: $400000.00

Federal Taxes: $120000.00

Net Pay: $280000.00


Output file:

1; ab c; $280000.00

...

package payroll;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

public class Employee {

    private int id;
    private String name;
    private double wage;
    private double hoursWorked;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getWage() {
        return wage;
    }

    public void setWage(double wage) {
        this.wage = wage;
    }

    public double getHoursWorked() {
        return hoursWorked;
    }

    public void setHoursWorked(double hoursWorked) {
        this.hoursWorked = hoursWorked;
    }



    public String toString() {
        return null;

    }




    public static void main(String[] args) {
        File myFile = new File("newdata.txt");

        System.out.println(myFile);

        Employee employee1 = new Employee();
        employee1.getId();
        employee1.getName();
        employee1.getWage();
        employee1.getHoursWorked();


    }



}
spider249
  • 23
  • 4
  • `java.util.List lines = java.nio.file.Files.readAllLines(Paths.get("newdata.txt"), StandardCharsets.UTF_8);` – matoni Jun 18 '17 at 22:17
  • What exactly is the problem here? Are you asking how to read text from file? There are already tons of similar questions here already. Did you read some of them like [Reading a plain text file in Java](https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java?)? Anyway your text file looks like variation of CSV file. Search for parser for that format and set it up to use `; ` as separator and let it do its job. – Pshemo Jun 18 '17 at 22:18
  • @Pshemo the problem here is that I want to turn the input data from the file into the output format and file and I am stuck – spider249 Jun 19 '17 at 00:52

2 Answers2

1

Here's an implementation (might have smaller mistakes, haven't compiled it). Add this to the Employee class and replace your main() function with the one below:

public String position;


public String getPosition(){
    return position;
}

public void setPosition(String position){
    this.position = position;
}

public String toString() {
    return getID() + "; " + getName() + "; " + getPosition() + "; " + getWage() + "; " + getHoursWorked();
}




public static void main(String[] args) {
    Employee employee1 = new Employee();

    java.util.List<String> lines = java.nio.file.Files.readAllLines(Paths.get("newdata.txt"), StandardCharsets.UTF_8);
    for(int i=2; i < lines.size(); i++){
        String[] splittedData = lines.get(i).split("; ");
        employee1.setId(splittedData[0]);
        employee1.setName(splittedData[1]);
        employee1.setPosition(splittedData[2]);
        employee1.setWage(Double.parseDouble(splittedData[3]));
        employee1.setHoursWorked(Double.parseDouble(splittedData[4]));
    }
}

NOTE: I'm not going to do this one, as I'm not sure whether you need it, but this implementation will overwrite the details of employee1 if you have multiple lines in the newdata.txt. You can add a List<Employee> object before the for loop and add the new Employee objects in each iteration.

detus
  • 89
  • 9
  • im getting some errors at `lines.length` and `lines[i].split("; ")` – spider249 Jun 19 '17 at 01:29
  • I've edited the answer. I don't have my computer with me so I can't check it, if it doesn't work comment again and I'll have a look at it once I'm at home. – detus Jun 19 '17 at 16:25
0

first of all you need to read the text from the file and fill out your class with real data. There are multiple ways for doing it in java. I.e. you can create a BufferedReadStream and read the file line by line.

Than you need to split every line into elements, using ';'. as you described.

Some of elements will represent string values, like names, others integer and double values. You would nee to convert strings into those values, using something like Double.parseDouble(string).

When you done, you can calculate what you need and print results.

Serge
  • 8,185
  • 2
  • 14
  • 22