Jones, Mary 63 $73800.00
Hayes, Michael 25 $14000.00
Richardson, Haley 32 $101000.00
Perry, Kendra 40 $53500.00
Duck, Donald 60 $3000000.00
We have to read this into an employee array with employees having the constructor
public Employee ( String a, String b, int c, double salary2){
lastName = a;
firstName = b;
age = c;
salary = salary2;
}
I tried doing it this way but I'm starting to think i need to use some sort of a pattern, not only in the code where I fill the array but also in the counting employees method. Pretty much I'm just stuck on how to read the file with a specific format and receive two strings, an int and a double from each line.
Here is what I have that does not work
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class HW2Employees {
static String fileTyped;
static int numEmployees;
static Employee [] employees = new Employee [numEmployees];
public static void main(String[] args) throws IOException {
getName();
countEmployees();
//readFile();
System.out.print(numEmployees);
}
// get the name of the file
public static void getName() {
System.out.println("Enter the name of the file you want to use?");
Scanner scan = new Scanner(System.in);
fileTyped = scan.next();
}
//find number of employees
public static void countEmployees () throws IOException{
File f = new File(fileTyped);
Scanner scan = new Scanner(f);
//Find size of array
int count = 0;
while (scan.hasNextInt()){
count++;
}
numEmployees = count;
scan.close();
}
//Input file into Employee Array
public static void readFile() throws IOException{
File f = new File(fileTyped);
Scanner scanFile = new Scanner(f);
int line = 0;
if (scanFile.hasNext()){
String first = scanFile.next();
String last = scanFile.next();
int age = scanFile.nextInt();
double salary = scanFile.nextDouble();
employees[line] = new Employee ( first, last, age, salary);
line++;
}
}
}
Here is the employee class if it helps:
public class Employee {
private String lastName;
private String firstName;
private int age;
private double salary;
public Employee ( String a, String b, int c, double salary2){
lastName = a;
firstName = b;
age = c;
salary = salary2;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

New Topic/Question
Reply




MultiQuote






|