Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Sorting a text file into an array

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting a text file into an array

    The programs runs and outputs everything perfectly

    But there is one more thing that needs to be added and I dont know where to begin.
    I now need to sort the employees into 2 arrays (2018, 2017)
    Distinguish between Employee, Salesman, and Executive
    run that employees info through their respective class

    i.e. The very first line of the "Project1.txt" file should be displayed like this after program execution
    Name: Mazzeo, Brian
    Monthly Salary: 3000
    Annual Salary: 36000

    I have a text file "Project1.txt" and it is set up like this:
    2017 Employee Mazzeo, Brian 3000
    2017 Employee Reinhart, Erik 5000
    2017 Salesman Speck, Randy 3900 95000
    2018 Salesman Gries, Alan 4000 3000000
    2018 Executive McCoy, Alex 6950 50
    2017 Executive Johnson, Matt 8000 55



    import java.io.*;
    import java.util.*;
     
    /**@author Erik Reinhart
     * @date 9/3/2018
     *The purpose of this program is to read in a text file Project1.txt and separate it into 2 arrays sorted by year.
     *The program will read through the text and determine if the person on each line of the text file is either an 
     *Employee, Salesman, or Executive and then run their data through the respective classes. 
     *
     */
     
    public class Main {
     
    	public static void main(String[] args) throws IOException {
     
    		BufferedReader input = null;
    		Scanner scan = new Scanner(System.in);
     
    		//reads in text file Project1.txt
    		String fileLine;
            try {
                input = new BufferedReader(new FileReader("Project1.txt"));
                System.out.println("Employees:");
                // Read one Line using BufferedReader
                while ((fileLine = input.readLine()) != null) {
                    System.out.println(fileLine);
                }
            } catch (IOException io) {
                System.out.println("File IO exception" + io.getMessage());
            }finally {
                // Need another catch for closing the streams      
                try {               
                    if (input != null) {
                    input.close();
                }                
                } catch (IOException io) {
                    System.out.println("Issue closing the Files" + io.getMessage());
                }
            }
     
     
            //Class Testers
    		Employee emp = new Employee("Reinhart", "Erik", 2000);
    		Salesman sales1 = new Salesman("Reinhart", "Erik", 2000, 990000);
    		Salesman sales2 = new Salesman("Reinhart", "Erik", 2000, 1500000);
    		Executive exe1 = new Executive("Reinhart", "Erik", 2000, 50);
    		Executive exe2 = new Executive("Reinhart", "Erik", 2000, 55);
    		Executive exe3 = new Executive("Reinhart", "Erik", 2000, 45);
    		System.out.println(emp.empToString());
    		System.out.println(sales1.toString());
    		System.out.println(sales2.toString());
    		System.out.println(exe1.toString());
    		System.out.println(exe2.toString());
    		System.out.println(exe3.toString());
    	}//end main()
    }//end class Main
     
     
    /**@author Erik Reinhart
     * @date 9/3/2018
     * The Employee class is the Super class. It has the basic function of accepting a line read in from the text file
     * Project1.txt and then displaying the information. It holds the toString() template used by the two sub classes,
     * class Salesman and class Executive 
     *
     */
    public class Employee {
     
    	//Variables
    	private String firstName;
    	private String lastName;
    	private double monthlySalary;
    	private double annualSalary;
    	//End Variables
     
    	//Constructor
    	public Employee(String lastName, String firstName, double monthlySalary) {
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.monthlySalary = monthlySalary;
    	}//End Constructor
     
    	//Setters and Getters
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
    	public void setMonthlySalary(double monthlySalary) {
    		this.monthlySalary = monthlySalary;
    	}
    	public double getMontlySalary() {
    		return monthlySalary;
    	}
    	public void setAnnualSalary(double annualSalary) {
    		this.annualSalary = annualSalary;
    	}
    	public double getAnnualSalary() {
    		return annualSalary = monthlySalary * 12;
    	}//End Setters and Getters
     
     
    	//toString() template for all classes
    	public String toString() {
    		return ("\n\tName: " + lastName + ", " + firstName + "\n\tMonthly Salary: " + monthlySalary);
    	}//End toString
     
    	//Employee class toString() 
    	public String empToString() {
    		return this.toString() + "\n\tAnnual Salary: " + this.getAnnualSalary();
    	}//End Employee class only toString 
    }//End class Employee
     
     
    /**@author Erik Reinhart
     * @date 9/3/2018
     * The Salesman class reads in the last name, first name, monthly salary of the Salesman employee
     * and adds a new instance "annualsSales" from the text file Project1.txt. This class will compute the total 
     * commission earned by multiplying annualSales * commissionRate. It will then test the value against the max 
     * commission using a if/else statement. If the totalCommission <= maxCommission, return totalCommission. Else, 
     * return maxCommission The information will then be displayed
     */
    public class Salesman extends Employee {
     
    	//variables
    	private double annualSales;
    	private double totalCommission;
    	private double maxCommission;
    	private double commissionRate;
    	//end variables
     
    	//super constructor
    	public Salesman(String lastName, String firstName, double monthlySalary, double annualSales) {
    		super(lastName, firstName, monthlySalary);
    		this.annualSales = annualSales;
    	}//end super constructor
     
    	//Setters and Getters
    	public void setAnnualSales(double annualSales) {
    		this.annualSales = annualSales;
    	}
    	public double getAnnualSales() {
    		return annualSales;
    	}
    	public void setCommissionRate(double commissionRate) {
    		this.commissionRate = commissionRate;
    	}
    	public double getCommissionRate() {
    		return commissionRate = .02;
    	}
    	public void setTotalCommission(double totalCommission) {
    		this.totalCommission = totalCommission;
    	}
    	public double getTotalCommission() {
    		return totalCommission = (annualSales * this.getCommissionRate());
    	}
    	public void setMaxCommission(double maxCommission) {
    		this.maxCommission = maxCommission;
    	}
    	public double getMaxCommission() {
    		return maxCommission = 20000;
    	}
    	//End Setters and Getters
     
    	//Test to see if Salesman Commissions exceed the limit of 20,000
    	public double maxCommissionTest() {
    		if (this.getTotalCommission() <= this.getMaxCommission())
    			return this.getTotalCommission();
    		else
    			return this.getMaxCommission();
    	}//End test
     
    	//Adds Salesman class details to Employee.toString
    	public String toString() {
    		return super.toString() + "\n\tAnnual Sales: " + annualSales + "\n\tCommission: " + this.maxCommissionTest();
    	}//End toString
    }//End class Salesman
     
     
    /**@author Erik Reinhart
     * @ 9/3/2018
     * The Executive class reads in the last name, first name, and monthly salary of the Executive employee and adds
     * a new instance "stockPrice" from the text file Project1.txt. This class will test the stockPrice to determine 
     * if the Executive employee will receive a bonus of 30,000 by using a if/else statement. If, stockPrice > 50,
     * employee receives bonus. Else, employee receives noBonus. It is then displayed
     *
     */
    public class Executive extends Employee {
     
    	//Variables
    	private double stockPrice;
    	private double bonus;
    	private double noBonus;
    	//End Variables
     
    	//super constructor
    	public Executive(String lastName, String firstName, double monthlySalary, double stockPrice) {
    		super(lastName, firstName, monthlySalary);
    		this.stockPrice = stockPrice;
    	}//end super constructor
     
    	//Setters and Getters
    	public void setStockPrice(double stockPrice) {
    		this.stockPrice = stockPrice;
    	}
    	public double getStockPrice() {
    		return stockPrice;
    	}
    	public void setBonus(double bonus) {
    		this.bonus = bonus;
    	}
    	public double getBonus() {
    		return bonus = 30000;
    	}
    	public void setNoBonus(double noBonus) {
    		this.noBonus = noBonus;
    	}
    	public double getNoBonus() {
    		return noBonus;
    	}//End Setters and Getters
     
    	//Test to determine if the Executive gets a bonus or not
    	public double bonusTest() {
    		if (this.getStockPrice() > 50)
    			return this.getBonus();
    		else
    			return this.getNoBonus();
    	}//End Test
     
    	//Adds Executive class details to Employee.toString()
    	public String toString() {
    			return super.toString() + "\n\tStock Price: " + stockPrice + "\n\tBonus: " + this.bonusTest();
    	}//End toString
    }//End class Executive

    //Displayed after running program
    Employees:
    2017 Employee Mazzeo, Brian 3000
    2017 Employee Reinhart, Erik 5000
    2017 Salesman Speck, Randy 3900 95000
    2018 Salesman Gries, Alan 4000 3000000
    2018 Executive McCoy, Alex 6950 50
    2017 Executive Johnson, Matt 8000 55

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Annual Salary: 24000.0

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Annual Sales: 990000.0
    Commission: 19800.0

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Annual Sales: 1500000.0
    Commission: 20000.0

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Stock Price: 50.0
    Bonus: 0.0

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Stock Price: 55.0
    Bonus: 30000.0

    Name: Reinhart, Erik
    Monthly Salary: 2000.0
    Stock Price: 45.0
    Bonus: 0.0
    Last edited by Reino17; September 3rd, 2018 at 09:49 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I dont know how to connect all the pieces

    to use each of the other classes.
    Create an instance of a class with a new statement.
    Save the reference to that instance to use in calling its methods:
      SomeClass refToClass = new SomeClass(someArgs);   // create instance of class
      refToClass.aMethod();    //call one of its methods

    See the java tutorial for lots more: http://docs.oracle.com/javase/tutori...ybigindex.html


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont know how to connect all the pieces

    Thanks for the tip on coding Norm. Also, Thanks for teaching me how to preserve formatting by wrapping my code with
     

    But I have new questions about my code and where I have gone wrong. Original post has be re-edited with the new questions and the code wrapped

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I dont know how to connect all the pieces

    How are you debugging the code to see what the problems are?
    To see what the code is doing, add some print statements that print out the values of variables as they are used and as their values are changed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Sorting a text file into an array

    "But there is one more thing that needs to be added and I dont know where to begin.
    I now need to sort the employees into 2 arrays (2018, 2017)
    Distinguish between Employee, Salesman, and Executive
    run that employees info through their respective class"


    I think you need to take each line of input from your text file (in your while loop) and then do the following steps:
    1. split each line (by spaces) into an array of employee detail
    2. sort this array on the first field (the year)
    3. use an if or switch statement on your second field to identify employee type
    4. create each employee (by the type found above) using the other fields in your array.
    5. print out your employee detail


    try implementing this list and if you get stuck post your code and we can try and help

Similar Threads

  1. Chess Game. Code to move pieces.
    By Vas87thRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2014, 03:41 AM
  2. How do I print pieces of an image in a frame?
    By jtm429 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2013, 01:02 PM
  3. Replies: 16
    Last Post: November 21st, 2013, 08:49 AM
  4. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM

Tags for this Thread