I am working on a project that requires the main system, EmployeeSystem to be able to open a file with JFileChooser, and then write to that file, run a payroll system on it, and search it for a particular employee using a directory system. I am struggling right now to get the file output working. The method specifically that is not working is the addEmployee() method. If I comment that method out the class compiles perfectly, and I am able to select a file through JFilechooser. So I know that part is working fine. However when I attempt to compile with addEmployee in the code, I get compiler errors because it doesn't recognize outStream. I'm not very strong on try catch finally statements, as I just learned them today. I think that I set it up right but I'm not positive.

Help is appreciated.

Compiler errors

EmployeeSystem.java:122: cannot find symbol
symbol  : method write(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int)
location: class java.io.FileOutputStream
		outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, numberHours, hourlyRate);
		         ^
EmployeeSystem.java:126: cannot find symbol
symbol  : variable outStream
location: class EmployeeSystem
		outStream.close(); }
		^
EmployeeSystem.java:146: cannot find symbol
symbol  : method write(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int)
location: class java.io.FileOutputStream
		outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, monthlySalary);
		         ^
EmployeeSystem.java:150: cannot find symbol
symbol  : variable outStream
location: class EmployeeSystem
		outStream.close(); }
		^
4 errors

EmployeeSystem

/**
 * EmployeeSystem class works as 
 * the main controlling system for 
 * accessing full time and casual employee objects
 * CSCE 155 Fall 2008
 * Assignment 3
 * @author Jared Wiebelhaus
 * @version
 */
 
 // import statements
import java.io.*;
import java.util.*;
import javax.swing.*;

public class EmployeeSystem  {

		// private data members - variables
		private int userChoice; // holds the input value for the control statement of 1 - 4
		
		String current = System.getProperty("user.dir");
		
		File				mainFile, payroll;
		JFileChooser	chooser = new JFileChooser(current); // creates a new object of JFileChooser
		private int				status;
		private int				employeeType;
		private String			employeeID;
		private String			employeeName;
		private String			birthDate;
		private String			startDate;
		private String			city;
		private String			state;
		private String			zip;
		private int				numberHours;
		private int				hourlyRate;
		private int				monthlySalary;
		
		/** Constructor
		* initialize all private data members to appropriate values
		*/
		
		public EmployeeSystem() {
		
		this.userChoice = 0;
		
		} //end constructor
		
		/** This method allows the user to select a file
		* which will then be used in the rest of the program
		*/
		public void getFile() {
		status = chooser.showOpenDialog(null);
		
		if (status == JFileChooser.APPROVE_OPTION) {
			mainFile = chooser.getSelectedFile();
			} else {
			System.exit(0);
			}
		}// end getFile
		
		
		/** This method gets the input from the user
		* using the readInteger() scanner method
		*/
		public void getInput() {
		System.out.println("Please select one of the following options:");
		System.out.println("     1. Search current employee information");
		System.out.println("     2. Add new employee information");
		System.out.println("     3. Run payroll for employees");
		System.out.println("     4. Exit");
		//obtain the actual value from the user
		
		userChoice = readInteger();
		}
		
		public void runSystem() {
		
		if (userChoice == 1) {
		//run directory system
		}
		else if (userChoice == 2) {
		//Reference a method in the EmployeeSystem itself to write data
		}
		else if (userChoice == 3) {
		//Run payroll system and display the name of the created file to the user
		}
		else if (userChoice == 4) {
		System.exit(0);
		} else {
		System.out.println("Please enter a number 1 - 4");
		}
		}//end runSystem
		
		/** This method starts an output stream
		* and allowes the user to write a new employee to the file
		*/
		
		public void addEmployee() {
		System.out.println("Please enter the type of employee (enter 1 for casual laborer, 2 for full time):");
		employeeType = readInteger();
		if (employeeType == 1) {
		System.out.println("Please enter the employee ID:");
		employeeID = readString();
		System.out.println("Please enter the employee name:");
		employeeName = readString();
		System.out.println("Please enter date of birth:");
		birthDate = readString();
		System.out.println("Please enter date of employment:");
		startDate = readString();
		System.out.println("Please enter city");
		city = readString();
		System.out.println("Please enter state");
		state = readString();
		System.out.println("Please enter ZIP");
		zip = readString();
		System.out.println("Please enter number of hours worked per day by employee");
		numberHours = readInteger();
		System.out.println("Please enter hourly rate of employee (in dollars):");
		hourlyRate = readInteger();
			try {
		FileOutputStream outStream = new FileOutputStream(mainFile);
		outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, numberHours, hourlyRate);
		} catch (IOException e) {
		//error statement
		} finally {
		outStream.close(); }
		} else if (employeeType == 2) {
		System.out.println("Please enter the employee ID:");
		employeeID = readString();
		System.out.println("Please enter the employee name:");
		employeeName = readString();
		System.out.println("Please enter date of birth:");
		birthDate = readString();
		System.out.println("Please enter date of employment:");
		startDate = readString();
		System.out.println("Please enter city");
		city = readString();
		System.out.println("Please enter state");
		state = readString();
		System.out.println("Please enter ZIP");
		zip = readString();
		System.out.println("Please enter monthly salary of employee (in dollars)");
		monthlySalary = readInteger();
			try {
		FileOutputStream outStream = new FileOutputStream(mainFile);
		outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, monthlySalary);
		} catch (IOException e) {
		//error statement
		} finally {
		outStream.close(); }
		} else {
		System.out.println("Please enter either 1 or 2");
		}
		}// end addEmployee()
		
		
		 /**
     * This method displays a welcome message to the user.
     */
    public void displayWelcome()  {
        
    	/* Display a brief welcome message about the system */
    	System.out.println("Welcome to the CSE Employee Directory");
		System.out.println("You can view employee information, add employees, and run the payroll using this system");
		}
		
		 /**
     * This method displays a goodbye message to the user.
     */
    public void displayGoodbye()  {
        
        System.out.println("Thank you, Goodbye!");
        
    }  // end displayGoodbye

	 /**
     * This method reads an integer and
     * returns that integer to the caller of this method.
     */
    private int readInteger()  {
        
        int temp = 0;
        
        Scanner scanner = new Scanner(System.in);
        
        try {
            temp = scanner.nextInt();  // read integer
        }  catch (InputMismatchException ex) {
            System.out.println(ex);
        }
        
        return temp;
        
    }  // end readInteger
    
    /**
     * This method reads in a string and returns that string to the caller of
     * this method.
     */
    private String readString()  {
        
        String userInput = "";
        
        Scanner scanner = new Scanner(System.in);
        
        userInput = scanner.nextLine();
        
        return userInput;
        
    }  // end readString

	   /** This main method creates an object of 
		* the EmployeeSystem
		*/
      public static void main(String[] args) throws IOException {
        
      EmployeeSystem mySystem = new EmployeeSystem();
        mySystem.displayWelcome();
		  mySystem.getFile();
        mySystem.getInput();
		  mySystem.runSystem();
        mySystem.displayGoodbye();
                                    
    }  // end main
}  // end Class PayrollSystem
EmployeeSystem.java:122: cannot find symbol
symbol  : method write(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int)
location: class java.io.FileOutputStream
		outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, numberHours, hourlyRate);
		         ^

There is no such method as you try to use. FileOutputStream is only able to work with byte array, there fore before you pass data to it you need to convert it to byte array

EmployeeSystem.java:126: cannot find symbol
symbol  : variable outStream
location: class EmployeeSystem
		outStream.close(); }
		^

Declaration of outStream was done inside try statement therefore this object is available only there. You will need to make declaration outside, something like this would sort it out

FileOutputStream outStream ;
try {
	outStream = new FileOutputStream(mainFile);	
} catch (IOException e) {
//error statement
} finally {
	outStream.close(); }

The other two errors are similar as the above
Have look at this tutorial on the use of FileOutputStream, if it is still not clear you more then welcomed to ask more questions

Thanks a lot. I went and looked up a method to write the text directly to the file and decided to use FileWriter instead. It's working fine now except for one small problem, when it outputs value to the file it overwrites and erases everything that was in the file and adds the new data. How do I get it to only add on the next line? This is the new code I am using, and the file I am attempting to write to. Would I have to make it loop through each line till it finds a blank one? Or is there a simpler way?

And how would I make each value in the text file be separated by a single space? I'll probably figure this out in a bit, I'm hoping it's a simple formatting issue.

Thanks again.

Modified AddEmployee()

public void addEmployee() {
		System.out.println("Please enter the type of employee (enter 1 for casual laborer, 2 for full time):");
		employeeType = readInteger();
		if (employeeType == 1) {
		System.out.println("Please enter the employee ID:");
		employeeID = readString();
		System.out.println("Please enter the employee name:");
		employeeName = readString();
		System.out.println("Please enter date of birth:");
		birthDate = readString();
		System.out.println("Please enter date of employment:");
		startDate = readString();
		System.out.println("Please enter city");
		city = readString();
		System.out.println("Please enter state");
		state = readString();
		System.out.println("Please enter ZIP");
		zip = readString();
		System.out.println("Please enter number of hours worked per day by employee");
		numberHours = readInteger();
		System.out.println("Please enter hourly rate of employee (in dollars):");
		hourlyRate = readInteger();
		try {
        FileWriter outFile = new FileWriter(mainFile);
        PrintWriter out = new PrintWriter(outFile);
            // Write text to file
         out.println(employeeType);
             out.close();
        } catch (IOException e){
              e.printStackTrace();
          }

		} else if (employeeType == 2) {
		System.out.println("Please enter the employee ID:");
		employeeID = readString();
		System.out.println("Please enter the employee name:");
		employeeName = readString();
		System.out.println("Please enter date of birth:");
		birthDate = readString();
		System.out.println("Please enter date of employment:");
		startDate = readString();
		System.out.println("Please enter city");
		city = readString();
		System.out.println("Please enter state");
		state = readString();
		System.out.println("Please enter ZIP");
		zip = readString();
		System.out.println("Please enter monthly salary of employee (in dollars)");
		monthlySalary = readInteger();
		try {
        FileWriter outFile = new FileWriter(mainFile);
        PrintWriter out = new PrintWriter(outFile);
         out.print(employeeType);
			out.print(employeeName);
			out.print(employeeID);
             out.close();
        } catch (IOException e){
              e.printStackTrace();
          }
		} else {
		System.out.println("Please enter either 1 or 2");
		}
		}// end addEmployee()

Employees.txt

2 Jeffery Johnson A001 06/14/1978 08/01/2006 402-486-5104 Lincoln NE 68588 1400
1 Jeffery Smith B002 09/12/1973 01/03/1999 402-357-5104 Seward NE 68801 3 15
2 Jessica Smith A303 11/21/1974 02/14/2003 402-408-6693 Eagle NE 68347 2200
2 Jeremy Suing X001 06/05/1975 05/16/2005 402-472-1658 Lincoln NE 68588 50
1 Sally Smith B001 01/11/1980 08/01/2006 402-357-3798 Fremont NE 68321 9 14
2 John Johnson B010 06/14/1979 08/01/2000 402-486-5110 Lincoln NE 68588 1450
1 Sarah Smith B200 10/14/1974 01/31/1997 402-408-5104 Utica NE 68701 5 25
1 John Doe A678 03/28/1981 03/02/1998 303-804-1169 Littleton CO 80104 4 10
1 Tad Jacobs X222 07/01/1975 08/16/2003 719-555-5555 Denver CO 80237 7 13
2 Ella Roberts B501 09/04/1983 05/10/2006 402-472-1100 Lincoln NE 68521 2400
1 Ellen Smith A501 08/24/1982 03/19/2001 402-472-2200 Lincoln NE 68520 3 40
2 Bobby Smith B555 12/04/1983 10/10/2006 402-470-1200 Lincoln NE 68527 240

Both FileWriter and PrintWriter do inherit append() methods from Writer that will do it for you

> [snip]
> How do I get it to only add on the next line?
> [snip]
> Or is there a simpler way?

Thoroughly reading the documentation would help you in the long run. :-)

I read the documentation much more thoroughly this time, although I am not always able to understand it all. I then modified the code so that it appends to the end of the file, and the modified the variable assignments so that a space is added to the front of each. Now it prints mostly right, however I am having a hard time making it print all on the last line. If I just type out.print for each one, they all print on the same line as the bottom entry, if I make employeeType an out.println all the other variables print on a new line, but employeeType is still stuck on the second to last line appended to the old data. Any ideas as to how I can get it to print all the variables to the same new line?

out.print("\n");
out.print("\n");

out.print(System.getProperty("line.separator")); or just make the OP use the BufferedWriters ' newLine() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.