Reading Ascii Binary

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 3950 Views - Last Post: 18 March 2010 - 04:31 AM Rate Topic: -----

#1 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Reading Ascii Binary

Posted 11 March 2010 - 02:46 AM

//im trying to write a program that reads a data file and a txt file. Im a little lost ive been looking up ways to do this and i came up with this.


public class Employee {


	public class Department {
		
		private int employeeId; 
		private String firstName;
		private String lastName;
		private int hireDate;
		private float salary;
		private int vacationDays;
		
		public Department(int employeeId, String firstName, String lastName,
				int hireDate, float salary, int vacationDays) {
			super();
			this.employeeId = employeeId;
			this.firstName = firstName;
			this.lastName = lastName;
			this.hireDate = hireDate;
			this.salary = salary;
			this.vacationDays = vacationDays;
		}

		public int getEmployeeId() {
			return employeeId;
		}

		public void setEmployeeId(int employeeId) {
			this.employeeId = employeeId;
		}

		public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public int getHireDate() {
			return hireDate;
		}

		public void setHireDate(int hireDate) {
			this.hireDate = hireDate;
		}

		public float getSalary() {
			return salary;
		}

		public void setSalary(float salary) {
			this.salary = salary;
		}

		public int getVacationDays() {
			return vacationDays;
		}

		public void setVacationDays(int vacationDays) {
			this.vacationDays = vacationDays;
		}
		
		
		

	}

}



	import java.io.DataInputStream;
	import java.io.FileInputStream;
	import java.io.FileNotFoundException;
	import java.io.IOException;

	public class EmployeeReader {
	
	


	
		public DataInputStream in;
		
		public EmployeeReader(String fileName) throws FileNotFoundException {
			in = new DataInputStream(new FileInputStream("accounts.dat"));
		}
		
		public Employee readAccount() throws IOException {
			if (in.available() == 0) {
				return null;
			}
			
		 int employeeId  = in.readInt(); 
		 String firstName = in.readUTF();
		 String lastName = in.readUTF();
		 int hireDate  = in.readInt();
		 float salary = in.readFloat();
		 int vacationDays  = in.readInt();
			
			
			
			Employee account = new Employee(employeeId, firstName, lastName, hireDate, salary, vacationDays);
			
			return account;
			
		}
		
		public void close() throws IOException {
			in.close();
		}

	}




}


public class Department {
	
	private String departmentName;
	private String managersName;
	private int numberOfEmployees;
	private int vacationDays;
	public Department(String departmentName, String managersName,
			int numberOfEmployees, int vacationDays) {
		super();
		this.departmentName = departmentName;
		this.managersName = managersName;
		this.numberOfEmployees = numberOfEmployees;
		this.vacationDays = vacationDays;
	}
	
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	public String getManagersName() {
		return managersName;
	}
	public void setManagersName(String managersName) {
		this.managersName = managersName;
	}
	public int getNumberOfEmployees() {
		return numberOfEmployees;
	}
	public void setNumberOfEmployees(int numberOfEmployees) {
		this.numberOfEmployees = numberOfEmployees;
	}
	public int getVacationDays() {
		return vacationDays;
	}
	public void setVacationDays(int vacationDays) {
		this.vacationDays = vacationDays;
	}
	
	
	
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;



public class DepartmentReader {
	
	public BufferedReader in;
	
	public DepartmentReader(String fileName ) throws FileNotFoundException {
		in = new BufferedReader(new FileReader("depts.txt"));
	}
	
	public Department readAccount() throws IOException {
		String line = in.readLine();
		
		if (line == null) {
			return null;
		}
		
		String[] parts = line.split(",");
		
		String departmentName  = parts[1];
		String managersName  = parts[2];
		int numberOfEmployees =  Integer.parseInt(parts[0]);
		int vacationDays  = Integer.parseInt(parts[0]);
		
		
		
		Department account = new Department(departmentName,  managersName,numberOfEmployees, vacationDays);
		
		return account;
		
	}
	
	public void close() throws IOException {
		in.close();
	}

}

	


}

This post has been edited by hall882006: 11 March 2010 - 07:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading Ascii Binary

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 02:56 AM

You are on the right track here. So what's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 03:15 AM

Reformatted below

This post has been edited by hall882006: 11 March 2010 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 05:38 AM

Reformatted below

Attached File(s)

  • Attached File  Depts.txt (89bytes)
    Number of downloads: 114

This post has been edited by hall882006: 11 March 2010 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 07:38 AM

Ugh. Take the directions out of the code tags man. It's a pain to read them...
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10559
  • View blog
  • Posts: 39,068
  • Joined: 27-December 08

Re: Reading Ascii Binary

Posted 11 March 2010 - 07:42 AM

Can you please describe your problem(s), the input, and the desired output in the body of your post not in code tags and without attaching files? We don't particularly like downloads on DIC. Thanks for helping us help you! :)
Was This Post Helpful? 1
  • +
  • -

#7 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 04:10 PM

reformatted below

This post has been edited by hall882006: 11 March 2010 - 07:23 PM

Was This Post Helpful? -1
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Reading Ascii Binary

Posted 11 March 2010 - 04:35 PM

We would love to help you but it is extremely hard with the comments that havent included comment tags in them as this has been stated above.

like this

//problem...

/* the problem is that... */



Also could you improve the indentation of your code and have import statements right at the top.

This post has been edited by m-e-g-a-z: 11 March 2010 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10559
  • View blog
  • Posts: 39,068
  • Joined: 27-December 08

Re: Reading Ascii Binary

Posted 11 March 2010 - 06:44 PM

@hall882006: As I said above, code goes in code tags, not your problem, assignment, input, output, etc. By putting long lines of descriptions not related to commenting your code inside code tags, you are making it harder for us to read your problem(s).
Was This Post Helpful? 0
  • +
  • -

#10 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 07:19 PM

im sorry if im a little slow im new at this this should be a little more read friendly and right format

//Department of items contain in the file
public class Department {
	
	private String departmentCode;
	private String departmentName;
	private int managersEmployeeId;

	public Department(String departmentCode, String departmentName,
			int managersEmployeeId) {
		super();
		this.departmentCode = departmentCode;
		this.departmentName = departmentName;
		this.managersEmployeeId = managersEmployeeId;
	}
	
	public String getDepartmentCode() {
		return departmentCode;
	}
	public void setDepartmentCode(String departmentCode) {
		this.departmentCode = departmentCode;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	public int getManagersEmployeeId() {
		return managersEmployeeId;
	}
	public void setManagersEmployeeId(int managersEmployeeId) {
		this.managersEmployeeId = managersEmployeeId;
	}
	
	
	

}

//Try to get reader to read the file this is where my problem occurs

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class DepartmentReader {

	private BufferedReader in;
	
	public void openFile(String fileName)
	{
		
		try
		{
			in =  new BufferedReader(new FileReader(fileName));
		}
		catch (FileNotFoundException fileNotFoundException)
		{
			System.err.println("Error opening File.");
			System.exit(1);
		}
	}
	
	public Department readAccount() throws IOException {
		String line = in.readLine();
	
	if (line == null) {
		return null;
	}
	
	String[] parts = line.split(",");
	
	 String departmentCode = parts[0];;
	 String departmentName = parts[1];;
	 int managersEmployeeId = Integer.parseInt(parts[3]);
	
	
	Department account = new Department(departmentCode, departmentName, managersEmployeeId);
	
	return account;
	
}
	
	
		
	
	public void close() throws IOException {
			in.close();
		}
	
	
	
	
	
}
	
	
//Try to see if its reading my file so i made a test main

import java.io.FileNotFoundException;
import java.io.IOException;

public class DumpFiles {

	public static void main(String[] args) {
		DumpFiles dumpAccount = new DumpFiles();
		dumpAccount.dumpAccounts("Depts.txt");
	}

	private void dumpAccounts(String fileName) {
		try {
			DepartmentReader readAccounts = new DepartmentReader();
			Department account;

			while ((account = readAccounts.readAccount()) != null) {
				System.out.println(account);
			}

			readAccounts.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}


My problem is it wont compile now to what i want and it doesnt seem to read my Depts.txt file i tried switching alot of things around but it still seems to not work. Heres is what im trying to accomplish

Produce a department report from an employee file (Employee.dat) and a department file (Depts.txt). Both file are on the I drive under I:\dtsmith\cosc210 and uploaded to Moodle.
Input:
Employee file - binary with the following fields:
Employee ID – 3 bytes ASCII
Employee/Contractor Indicator – 1 byte ASCII ‘E’ for employee, ‘C’ for contractor
Department Code – 1 byte ASCII
First Name – 20 bytes ASCII, blank padded
Last Name – 15 bytes ASCII, blank padded
Salary – float single precision
Hire Date – 1 byte binary identifies the month, 1 byte binary identifies the day, 2 bytes binary identifies the year
Vacation Days – 2 bytes signed binary
Training – 1 byte

Department file - comma delimited text. Each line in the file represents a department. The comma delimited fields are:
Department Code
Department Name
Employee ID of the manager for the department
Output:
Print a report listing of Employees by Department. For each department, print the department name, manager’s name, number of employees in the department, and the sum of the vacation days of its employees. For each employee, print the employee ID, name, hire date, salary, and vacation days. A sample report is provided below:

Sales Department

Manager: John Smith
Staff size: 2
Vacation Days: 15

ID Employee Name Hire Date Salary Vacation Days
127 John Smith 10/12/1996 $ 80.50 10
382 Mary Brown 2/30/2001 $ 50.00 5

Marketing Department

Manager: Sue Green
Staff size: 1
Vacation Days: 15

ID Employee Name Hire Date Salary Vacation Days
234 Sue Green 12/12/1990 $180.50 15


Hopefully i can get pointed in the right direction
Was This Post Helpful? 0
  • +
  • -

#11 tectonic.software  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 88
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 11 March 2010 - 08:44 PM

Ok, these are your current problems:
  • Your never doing openAccount in the constructor of DepartmentReader, So your BufferedReader in == null. Causing a NullPointerException.
  • int managersEmployeeId = Integer.parseInt(parts[3]); .... parts.length == 3, So the max that array can go in the code is 2.
  • Object.toString() or Department.toString() returns getClass().getName() + '@' + Integer.toHexString(hashCode()), and when you do System.out.println(account); your basically doing System.out.println(account.toString());, Fix this by mabye adding a method in Department that prints everything or returns a string so you can print everything?

Was This Post Helpful? 1
  • +
  • -

#12 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 12 March 2010 - 12:11 AM

From what i have for my departmentReader would i do the same thing for a dat file for example something that looks like this

091EAStover Nick Ap 
Τ (092CEClawson Virginia A‚ Τ 5093ECClawson

or am i supposed to convert that file first to a txt file. Heres my code so i can show you

public class Employee {

	private int employeeId;
	private String employeeContractor;
	private String departmentCode;
	private String firstName;
	private String lastName;
	private double salary;
	private int hireDate;
	private int vacationDays;
	private int training;

	public Employee(int employeeId, String employeeContractor,
			String departmentCode, String firstName, String lastName,
			double salary, int hireDate, int vacationDays, int training) {
		super();
		this.employeeId = employeeId;
		this.employeeContractor = employeeContractor;
		this.departmentCode = departmentCode;
		this.firstName = firstName;
		this.lastName = lastName;
		this.salary = salary;
		this.hireDate = hireDate;
		this.vacationDays = vacationDays;
		this.training = training;
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmployeeContractor() {
		return employeeContractor;
	}

	public void setEmployeeContractor(String employeeContractor) {
		this.employeeContractor = employeeContractor;
	}

	public String getDepartmentCode() {
		return departmentCode;
	}

	public void setDepartmentCode(String departmentCode) {
		this.departmentCode = departmentCode;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public int getHireDate() {
		return hireDate;
	}

	public void setHireDate(int hireDate) {
		this.hireDate = hireDate;
	}

	public int getVacationDays() {
		return vacationDays;
	}

	public void setVacationDays(int vacationDays) {
		this.vacationDays = vacationDays;
	}

	public int getTraining() {
		return training;
	}

	public void setTraining(int training) {
		this.training = training;
	}

	public String toString() {
		return ("EmployeeId(" + employeeId + ") Employee(" + employeeContractor
				+ ") (" + departmentCode + ") (" + firstName + ") (" + lastName
				+ ") (" + salary + ") (" + hireDate + ") (" + vacationDays
				+ ") (" + training + ")");
	}

}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class EmployeeReader {

	public DataInputStream in;

	public EmployeeReader(String fileName) throws FileNotFoundException {
		in = new DataInputStream(new FileInputStream(fileName));
	}

	public Employee readAccount() throws IOException {
		if (in.available() == 0) {
			return null;
		}

		int employeeId = in.readInt();
		String employeeContractor = in.readUTF();
		String departmentCode = in.readUTF();
		String firstName = in.readUTF();
		String lastName = in.readUTF();
		double salary = in.readDouble();
		int hireDate = in.readInt();
		;
		int vacationDays = in.readInt();
		;
		int training = in.readInt();
		;

		Employee account = new Employee(employeeId, employeeContractor,
				departmentCode, firstName, lastName, salary, hireDate,
				vacationDays, training);

		return account;

	}

	public void close() throws IOException {
		in.close();
	}

}


	import java.io.FileNotFoundException;
	import java.io.IOException;


public class DumpFiles2 {
	
	
		public static void main(String[] args) {
			DumpFiles2 dumpAccount = new DumpFiles2();
			dumpAccount.dumpAccounts("Employee.dat");
		}

		private void dumpAccounts(String fileName) {
			try {
				EmployeeReader readAccounts = new EmployeeReader(fileName);
				Employee account;

				while ((account = readAccounts.readAccount()) != null) {
					System.out.println(account);
				}

				readAccounts.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

Was This Post Helpful? 0
  • +
  • -

#13 hall882006  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 89
  • Joined: 12-February 10

Re: Reading Ascii Binary

Posted 12 March 2010 - 09:17 AM

i having trouble reading a binary file i know the pattern of the stuff i need just dont know how to extract it. Example this is what the data file looks like:

091EAStover              Nick           Ap  
Ô (092CEClawson             Virginia       A‚  Ô 5093ECClawson             John           AH  Ô 4094EENoel                Beverly        A–  Ò )095CEClawson             Colin          AŒ  
Ò ,096ESCochran             Beverly        A4  Ð #097CACochran             John           A\  	Ô 0098ESCochran             Ricky          A   


this is supposed to be the binary fields in the file:

Employee ID – 3 bytes ASCII
Employee/Contractor Indicator – 1 byte ASCII ‘E’ for employee, ‘C’ for contractor
Department Code – 1 byte ASCII
First Name – 20 bytes ASCII, blank padded
Last Name – 15 bytes ASCII, blank padded
Salary – float single precision
Hire Date – 1 byte binary identifies the month, 1 byte binary identifies the day, 2 bytes binary identifies the year
Vacation Days – 2 bytes signed binary
Training – 1 byte


how would i go about getting this from the dat file to used to make a report
Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Reading Ascii Binary

Posted 12 March 2010 - 11:03 AM

You would need to use I/O Streams.

Heres a link I/O Streams
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,703
  • Joined: 24-September 08

Re: Reading Ascii Binary

Posted 12 March 2010 - 11:53 AM

This seems kinda fishy to me.... or should I say "phishy"? :ph34r:
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »