We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,325 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trouble reading in text file

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:TEMPeclipse-java-indigo-SR1-win32eclipseEmployee.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
3
Contributors
2
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
swy
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

Can you edit your post and wrap the code in code tags? Use the [CODE} icon above the input box.
The unformatted code is hard to read.

Where do you print the lines you show?
Where do you give values to the variables that are being printed as null and 0?
Where and when does the program execute the code where the variables are given values?
If you do NOT execute any code that gives the variables values, their values will be the default value.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0658 seconds using 2.72MB