I'm trying to read in the following text file:
Jim Nasium mgr Fitness & Leisure 45000 Lazy Susan mgr Home Furnishings 55000 Gene Theory mgr Fine Jewelry 10000
And I have the following code:
public abstract class Employee { private String name; private double grossPay; private double netPay; private final double TAX_RATE = 28.0;
/* Constructors */ public Employee() { }
abstract public void computePay();
public double computeTax() { return (TAX_RATE/100.0)*grossPay; }
public String toString() { String retString = ""; retString = "Name: " + this.getName() + "\n"; retString += "Gross Salary: " + this.grossPay + "\n"; retString += "Tax: " + computeTax() + "\n"; retString += "Net Salary: " + this.netPay + "\n"; return retString; } public void setNetPay(double pay) { this.netPay = pay; } public double getNetPay() { return netPay; } public void setGrossPay(double pay) { this.grossPay = pay; } public double getGrossPay() { return grossPay; }
public void setName(String name) { this.name = name; }
public String getName() { return name; } }
public class Managers extends Employee implements Bonuses { private String department; private double bonus; private final double BONUS_RATE = 1.0; private String name; private String jobType; private Double income; private double tax; /* Constructors */ public Managers() { } public void inputInfo(String s, String inL) { if(s.equals("name")) name = inL; else if(s.equals("title")) jobType = inL; else if(s.equals("department")) department = inL; else if(s.equals("salary")) income = Double.parseDouble(inL);
} @Override public double calcBonus() { this.setGrossPay((income/24) + this.bonus); this.bonus = (income/24)*(BONUS_RATE/100); return bonus; } @Override public void computePay() { this.setGrossPay((income/24) + this.bonus); tax = this.computeTax(); this.setNetPay(income/24 + this.bonus - tax); }
public String printInfo() { String s = ""; s += name + " " + jobType + " " + income + " " + this.getGrossPay() + " " + this.getNetPay() + " " + tax; return s; } }
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner;
public class Payrolls { public static void main(String[] args) { Managers mgr = new Managers(); System.out.println(mgr.printInfo()); //inL = in.readLine(); } public static void getInfo()throws FileNotFoundException { Scanner inF = new Scanner(new File("C:\\TEMP\\eclipse-java-indigo-SR1-win32\\eclipse\\Employee.txt")); while(inF.hasNextLine( )) { Managers mgr = new Managers(); mgr.inputInfo("name", inF.nextLine( )); mgr.inputInfo("title", inF.nextLine( )); mgr.inputInfo("department", inF.nextLine( )); mgr.inputInfo("salary", inF.nextLine( )); mgr.calcBonus(); mgr.computePay(); } } }
Yet when I run the program, I get the following output:
null null null 0.0 0.0 0.0
try something like this instead:
fileToRead = new Scanner(new File("c:\\ggg.txt")); //point the scanner method to a file
for (String line; fileToRead.hasNextLine() && (line = fileToRead.nextLine()) != null; ) {
System.out.println(line);//print each line as its read
}
as you can see mine is different in the way that it will only print the line if a line does exist and its not null, which might be whats giving your grief. where i print the line is where you would:
mgr.inputInfo("name",line);//not i dont use nextLine() again else it will skip the last line, i use an assigned variable in the for statement