reading from a file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: reading from a file

  1. #1
    Join Date
    Apr 2006
    Posts
    14

    help with project?

    can someone help I am stuck with this final project
    i am trying to get input from the text file then use the input to calculate grosspay with employee class and output it with name and id

    Code:
    import java.io.*;
    import java.util.*;
    
    
    public class main {
    
    	// main application
    	public static void main(String[] args) throws IOException {
    
            //variables
            String currentLineOfFile;
            int employeeID;
            int employeePayGrade;
            int hoursWorked;
    
    
            //reading input file
    		String fileName = "employeeinfo.txt";
    
    
            //fileName
    		File file = new File(fileName);
    
            //file stream
    		Scanner fileIn = new Scanner(file);
    
    		//do-while to process text file until no more lines
    		do {
    
    			 //for loop to populate array
                 for (int a = 0; a < 9; a++)
    
                     {
    
                        //for loop to create a each new object for employee
                        for (int b = 0; b < 4; b++)
    
                            {
                              //reading line
                              currentLineOfFile = fileIn.nextLine();
    
    			              Scanner scan = new Scanner(currentLineOfFile);
    
                                switch (b)
    
                                	{
    
                                    case (0):
    
                                    	{
                                         employee employeeObjectsArray[] = new employee();
                                         employeeObjectsArray[a].setEmployeeName(currentLineOfFile);
                                         break;
                                    	}
    
                                    case (1):
                                    	{
                						 employeeID = scan.nextInt();
    									 employeeObjectsArray[a].setEmployeeId(employeeID);
                                         break;
                                    	}
    
                                    case (2):
                                    	{
                                         employeePayGrade = scan.nextInt();
                                         employeeObjectsArray[a].setgradeLevel(employeePayGrade);
                                         break;
                                    	}
    
                                    case (3):
                                    	{
    								     hoursWorked = scan.nextInt();
    									 employeeObjectsArray[a].setOvertimeHours(hoursWorked);
                                         break;
                                    	}
                                	}
                            }
    
    
                        employeeObjectsArray[a].calculateGrossPay();
    
                        System.out.println(employeeObjectsArray[a].getEmployeeName());
                        System.out.println(employeeObjectsArray[a].getEmployeeId());
                        System.out.println(employeeObjectsArray[a].getGrossPay());
                     }
    
                 //display total overtime hours
                 System.out.println(employeeObjectsArray[0].getTotalOvertimeHours());
    
    		} while (fileIn.hasNextLine());
    
    	//close file after no more input
    	fileIn.close();
    
     	}  // end main method
    
    }
    Code:
    import java.io.*;
    import java.util.*;
    
    
    public class employee {
    
    	//variable
    	static int totalOvertimeHours = 0;
    
    	//instance variables
    	private String employeeName;
    	private static int employeeId;
    	private static int employeeGradeLevel;
    	private static int overTimeHours;
    	private static double grossPay;
    
    
     	//overloaded constructor
    	public void employee(String employeeName, int employeeId, int employeeGradeLevel, int overTimeHours) {
    
    		employeeId= 000000000;
    	    employeeGradeLevel = 0;
    	    overTimeHours= 0;
    
    	  	}
    
    
    	  //mutator
    	  public void setemployeeName(String name) {
    
    	      employeeName = name;
    	  }
    
    
    	  //mutator
    	  public void setemployeeId(int Id) {
    
    	      employeeId = Id;
    	  }
    
    
    	  //mutator
    	  public void setgradeLevel(int grade) {
    
    	      employeeGradeLevel = grade;
    	  }
    
    
    	  //mutator
    	  public void setovertimeHours(int hours) {
    
    	      overTimeHours = hours;
    	  }
    
    
    	  //accessor
    	  public String getemployeeName() {
    
    	      return employeeName;
    	  }
    
    
    	  //accessor
    	  public static int getemployeeId() {
    
    	      return employeeId;
    	  }
    
    
    	  //accessor
    	  public static int getgradeLevel() {
    
    	      return employeeGradeLevel;
    	  }
    
    
    	  //accessor
    	  public static int getoverTimeHours() {
    
    	      return overTimeHours;
    	  }
    
    
    	  //accessor that calculates total gross pay
    	  public static void calculateGrossPay() {
    
    	  	  grossPay = ( 80 * employee.getgradeLevel() ) + employee.getoverTimeHours() * ( 1.5 * employee.getgradeLevel() );
    
    	  }
    
    }
    I am trying to read input from this txt file into main and calculate with emplyee class

    Text file
    Code:
    Smith
    654789345
    1
    8
    Jones
    456983209
    2
    10
    Anders
    989876123
    1
    15
    Estes
    237847360
    4
    7
    Costa
    115387243
    2
    20
    Showen
    223456732
    2
    18
    Roberts
    574839280
    3
    12
    Able
    427836410
    1
    10
    Atkins
    349856232
    2
    7
    Seimens
    328978455
    4
    5
    Last edited by Zahrber; 05-12-2006 at 12:10 AM.

  2. #2
    Join Date
    Apr 2006
    Posts
    14
    these are the errors i am getting


    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:59: incompatible types
    found : employee
    required: employee[]
    employee employeeObjectsArray[] = new employee();
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:60: cannot find symbol
    symbol : method setEmployeeName(java.lang.String)
    location: class employee
    employeeObjectsArray[a].setEmployeeName(currentLineOfFile);
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:67: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    employeeObjectsArray[a].setEmployeeId(employeeID);
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:74: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    employeeObjectsArray[a].setgradeLevel(employeePayGrade);
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:81: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    employeeObjectsArray[a].setOvertimeHours(hoursWorked);
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:88: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    employeeObjectsArray[a].calculateGrossPay();
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:90: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    System.out.println(employeeObjectsArray[a].getEmployeeName());
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:91: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    System.out.println(employeeObjectsArray[a].getEmployeeId());
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:92: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    System.out.println(employeeObjectsArray[a].getGrossPay());
    ^
    C:\Documents and Settings\Compaq_Administrator\Desktop\New Folder\New Folder\main.java:96: cannot find symbol
    symbol : variable employeeObjectsArray
    location: class main
    System.out.println(employeeObjectsArray[0].getTotalOvertimeHours());
    ^
    10 errors

    Tool completed with exit code 1

  3. #3
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    employee employeeObjectsArray[] = new employee();
    employeeObjectsArray[a].setEmployeeName(currentLineOfFile);
    you have to initialize the array correctly:
    1. prepare the array with a given size (yes, initialization of arrays needs to know the size of the array):
    employee employeeObjectsArray[] = new employee[10];
    2. you have to create an instance of your employee class for each arrayposition.
    the initialization of the array above just reserves the space for the array, it does NOT create the objects for the array:
    for (int i=0;i<employeeObjectsArray.length;i++)
    employeeObjectsArray[i] = new employee();

    if you don't know how many employees will be loaded from file, so you don't know how big the initial array must be. in this case you will have to use a dynamic growing datastructure.
    in this case i suggest you to use the linked list:
    List employeeslist = new LinkedList();
    employee current = new employee();
    current.setEmployeeName(currentLineOfFile);
    ... (when finished with this employee, add it to your list)
    employeeslist.add(current);

    when finished reading al employeesm you can convert the list to an array object:

    employe [] employeeObjectsArray = (employee[]) employeeslist.toArray(new employee[0]);

Similar Threads

  1. Replies: 14
    Last Post: 11-29-2005, 07:56 AM
  2. Reading and Writing to a file
    By Fergy25 in forum Java
    Replies: 1
    Last Post: 11-14-2005, 05:28 PM
  3. Replies: 2
    Last Post: 11-14-2001, 01:49 PM
  4. Replies: 3
    Last Post: 08-23-2001, 12:01 PM
  5. How to use Java to lock a file
    By Cynthia Leslie in forum Java
    Replies: 3
    Last Post: 06-09-2001, 07:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center