0

I have a text file formatted like this:

[Employee ID], [Full Name], [Rate of Pay]

A couple sample line looks like:

32483, William Ellis, 25.50

58411, Jonathan Ian Morris, 15.00 

I'm trying to read all the employees in a text file and create objects of each one that will store the names, ID's, and wages in a hashmap. I'm taking EmpID as a string, the employee names as strings, and the wages as doubles. My problem is that it reads the EmpID's as a string including the "," then it will read the first name as the name variable, then attempt to take the middle or last name as a double with the "," also attached. So for instance it would come off as:

empID: "32483,"

fullName: "William"

wage: Ellis (which produces an error)

while(inputFile.hasNext()) {
        empID = inputFile.next();
        fullName = inputFile.next();
        wage = inputFile.nextDouble();
        empmap.put(empID, new Employee(empID, fullName, wage));
    }

I figured I could try first and last name, but some have middle names or go by simply one name. I also considered the possibility of taking each character in character by character and concatenates them while using the "," as a sort of null character that lets the program know that everything that came before it is one string and a new string is about to begin but before I try something like that I figure there has to be an easier solution.

  • 1
    Scanner.next(), by default, uses white-space as a separator. So, change the separator. Or read the next line, and use String.split() to split on the comma character. Or use a CSV parser. – JB Nizet Jan 18 '18 at 10:18

4 Answers4

0

My preference would be to use Files.lines to get a stream containing each line of the file, then split each line based on the delimiter:

public class Main
{
    public static void main(final String... args) throws Exception
    {
        Files.lines(Paths.get("path/to/my/file"))
             .forEach(Main::processLine);
    }

    private static void processLine(final String line)
    {
        final String[] tokens = line.split(",");
        final String id = tokens[0].trim();
        final String name = tokens[1].trim();
        final String rateOfPay = tokens[2].trim();

        //...
    }
}
Michael
  • 34,340
  • 9
  • 58
  • 100
0

Assuming each line has a specific, constant format, you can do the following:

read the file line by line. for each line:

  1. Split the string in each line. (with the comma ,)
  2. parse the separate parts of the split string
  3. put into some collection

Reading a file is quite simple. This answer can be a reference.

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    String line = br.readLine();

    while (line != null) {

        line = br.readLine();
        String[] splitStr=line.split(", ");
        if(splitStr.length==3){
            int empID=Integer.parseInt(splitStr[0]);
            String fullName=splitStr[1];
            double wage=Double.parseDouble(splitStr[2]);
        }
    }

} finally {
    br.close();
}
ItamarG3
  • 3,846
  • 6
  • 28
  • 42
0

Try this code :

 public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("employee.txt"));
        input.useDelimiter(",|\n");

        Employee[] emps = new Employee[0];
        while(input.hasNext()) {
            int id = input.nextInt();
            String fullname = input.next();
            double payrate = Double.valueOf(input.next().substring(1));

            Employee newEmp = new Employee(fullname, payrate, id);
            emps = addEmployee(emps, newEmp);
        }

        for (Employee e : emps) {
            System.out.println(e);
        }
    }

    private static Employee[] addEmployee(Employee[] emps, Employee empToAdd) {
        Employee[] newEmps = new Product[products.length + 1];
        System.arraycopy(products, 0, newProducts, 0, products.length);
        newEmps[newEmps.length - 1] = empToAdd;

        return newEmps;
    }

    public static class Employee {
        protected int id;
        protected String fullname;
        protected double payrate;

        private static NumberFormat formatter = new DecimalFormat("#0.00");

        public Product(String n, double p, int i) {
            id = i;
            fullname= n;
            payrate = p;

        }
Léo R.
  • 3,041
  • 1
  • 8
  • 21
0

You can use Split to handle this. In your example:

String [] data = inputFile.split(","); 
empId = Integer.parseInt(data[0]);
fullname = data [1]; 
(...)

Good luck