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 2 of 2

Thread: ArrayList problem

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

    Default ArrayList problem

    **At the very bottom is my question**

    I have a text file called "Employees.txt" and it is set up like this:

    2014 Employee Weaver,Earl 2000
    2015 Salesman Murray,Eddie 3000 1000000
    2015 Executive Jones,Adam 5000 55
    2014 Salesman Robinson,Frank 3000 999999
    2015 Salesman Palmer,Jim 3000 1500000
    2014 Executive Robinson,Brooks 5000 50
    2014 Executive Ripken,Cal 5000 45

    import java.io.*;
    import java.util.*;
     
    /**@author John Doe
     * @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;
    		BufferedWriter output = null;
    		String strBlank = "";
    		String strEmployee = "Employee";
    		String strSalesman = "Salesman";
    		String strExecutive = "Executive";
    		String str2014 = "2014";
    		String str2015 = "2015";
    		List<String> array2014 = new ArrayList<String>();
    		List<String> array2015 = new ArrayList<String>();
    		Scanner scan = new Scanner(System.in);
     
    		//reads in text file Project1.txt
    		String fileLine;
            try {
                input = new BufferedReader(new FileReader("Employees.txt"));
                output = new BufferedWriter(new FileWriter("EmployeesWriter.txt"));
                System.out.println("Employees.txt File:");
                // Read one Line using BufferedReader
                while ((fileLine = input.readLine()) != null) {
                    int yearSearch = fileLine.length();
                    int search2014 = str2014.length();
                    int search2015 = str2015.length();
                    boolean foundIt = false;
     
                    for (int i = 0; i <= (yearSearch - search2014); i++) {
                          if (fileLine.regionMatches(i, str2014, 0, search2014)) {
                             foundIt = true;
                             array2014.add(fileLine);
                          }
                          else if(fileLine.regionMatches(i,  str2015, 0, search2015)) {
                        	  foundIt = true;
                        	  array2015.add(fileLine);
                          }
                    }
     
     
                    if(!foundIt) {
                    	System.out.println("not found");
                    }
     
                	System.out.println(fileLine);
                    output.write(fileLine + "\r\n");
                }
            } 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();
                    if (output != null) {
                        output.close();
                    } 
                    }
                }
     
                    catch (IOException io) {
                    System.out.println("Issue closing the Files" + io.getMessage());
                }
            }
            System.out.println("\n2014 Employees");
            for(int j = 0; j < array2014.size(); j++) {
                System.out.println("\n" + (j + 1) + "\t" + array2014.get(j));
                System.out.println("\t\tAnnualSalary: ");
            }
            System.out.println("\n2015 Employees");
            for(int k = 0; k < array2015.size(); k++) {
      		    System.out.println("\n" + (k + 1) + "\t" + array2015.get(k));
      		    System.out.println("\t\tAnnualSalary: ");
      	    }
     
     
            //Class Testers, these just make sure my calculations work out correctly
    		Employee emp = new Employee("Doe", "John", 2000);
    		Salesman sales1 = new Salesman("Doe", "John", 2000, 999999);
    		Salesman sales2 = new Salesman("Doe", "John", 2000, 1500000);
    		Executive exe1 = new Executive("Doe", "John", 2000, 50);
    		Executive exe2 = new Executive("Doe", "John", 2000, 55);
    		Executive exe3 = new Executive("Doe", "John", 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 John Doe
     * @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 annualSalary() {
    		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.annualSalary();
    	}//End Employee class only toString 
    }//End class Employee

    /**@author John Doe
     * @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
     
    	public double annualSalary(double annualSalary) {
    		return annualSalary;
    	}
     
    	//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 John Doe
     * @ 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
     
    	public double annualSalary(double annualSalary) {
    		return annualSalary;
    	}
     
    	//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

    *******************program output*************************
    Employees.txt File:
    2014 Employee Weaver,Earl 2000
    2015 Salesman Murray,Eddie 3000 1000000
    2015 Executive Jones,Adam 5000 55
    2014 Salesman Robinson,Frank 3000 999999
    2015 Salesman Palmer,Jim 3000 1500000
    2014 Executive Robinson,Brooks 5000 50
    2014 Executive Ripken,Cal 5000 45

    2014 Employees

    1 2014 Employee Weaver,Earl 2000
    AnnualSalary: *******problem area*******

    2 2014 Salesman Robinson,Frank 3000 999999
    AnnualSalary: *******problem area*******

    3 2014 Executive Robinson,Brooks 5000 50
    AnnualSalary: *******problem area*******

    4 2014 Executive Ripken,Cal 5000 45
    AnnualSalary: *******problem area*******

    2015 Employees

    1 2015 Salesman Murray,Eddie 3000 1000000
    AnnualSalary: *******problem area*******

    2 2015 Executive Jones,Adam 5000 55
    AnnualSalary: *******problem area*******

    3 2015 Salesman Palmer,Jim 3000 1500000
    AnnualSalary: *******problem area*******

    Name: Doe, John
    Monthly Salary: 2000.0
    Annual Salary: 24000.0

    Name: Doe, John
    Monthly Salary: 2000.0
    Annual Sales: 999999.0
    Commission: 19999.98

    Name: Doe, John
    Monthly Salary: 2000.0
    Annual Sales: 1500000.0
    Commission: 20000.0

    Name: Doe, John
    Monthly Salary: 2000.0
    Stock Price: 50.0
    Bonus: 0.0

    Name: Doe, John
    Monthly Salary: 2000.0
    Stock Price: 55.0
    Bonus: 30000.0

    Name: Doe, John
    Monthly Salary: 2000.0
    Stock Price: 45.0
    Bonus: 0.0
    **********************program output finish*************************

    2014 Employee Weaver,Earl 2000
    2015 Salesman Murray,Eddie 3000 1000000
    2015 Executive Jones,Adam 5000 55
    etc.

    Im going to use the same coding technique regionMatches() to determine if the employee type is an "Employee, Salesman, or Executive" as I did with sorting them into 2 different arrays categorized by year. and then run them through their respective classes.

    How do I locate the names of each employee up above in Italics?
    -They are all of different .lengths() and different names, so I cant use the same approach as with YEAR, and EMPLOYEE TYPE using a regionMatches().

    How do I locate and convert the numbers up above in bold underlined into doubles?
    -I know to use parseDouble() for the conversion
    -My problem is with locating the numbers in the String so that I can convert them and output the annual salaries in the spots called *******problem area***********

    I'm looking for the solution to my problem not a point in the right direction

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: ArrayList problem

    Instead of thinking about lengths, think about values separated by delimiters (in this case, one or more blank spaces). So read in a line
    and use the String.split() string method to return an array of fields. You will need to check the String documentation to learn more about split.

    Another way would be to use a Scanner instance to read the file. Then you can use next() or nextInt() or nextDouble() as appropriate.

    Regards,
    Jim

Similar Threads

  1. ArrayList problem.
    By ben1 in forum Collections and Generics
    Replies: 1
    Last Post: May 29th, 2014, 10:20 AM
  2. Arraylist get problem
    By soniabobit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2013, 06:40 PM
  3. ArrayList problem
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 11:48 AM
  4. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  5. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM