Output data to .txt file

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1169 Views - Last Post: 18 December 2011 - 07:09 AM Rate Topic: -----

#1 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Output data to .txt file

Posted 16 December 2011 - 10:00 PM

Hey guys, I'm looking to up my Java skills, right now I'm trying to outupt the data sent out from my for loops into a .txt file as it is shown in the Data output section of JCreator. I've searched a few websites for source code, but I haven't worked out how to actually implement it. My source code is below. Thanks in advance guys!
import java.util.*;
public class Working {

  public static void main(String[] args) {
    Employee[] employees = new Employee[6];
    Manager[] managers = new Manager[3];
    //ArrayList employees = new ArrayList();
    	employees[0] = new Employee(1, "Taylah Jayne", 2000, 50000.00, 1);
    	employees[1] = new Employee(2, "Hurp", 1981, 1000000.00, 3);
    	employees[2] = new Employee(3, "Hurp Durp", 2011, 10000.00, 1);
    	employees[3] = new Employee(4, "4chan", 2004, 0.00, 2);
    	employees[4] = new Employee(5, "m00t", 2004, 200000.00, 3);
    	employees[5] = new Employee(6, "Derpina", 2009, 15000.00, 1);
    	
    	managers[0] = new Manager(1, "Nathan Kreider", 2000, 500000.00, 5);
    	managers[1] = new Manager(2, "Durp", 2000, 50000.00, 4);
    	managers[2] = new Manager(3, "Durp Hurp", 1821, 180912.00, 5);
    		//employees.add(1, "Nathan Kreider", 2000, 500000.00, 5);
    	
    	System.out.println("Employees who have been working here for 10 years or more.");
    for(int i = 0; i < employees.length; i++) {
    	if(employees[i].getHireYear() <= 2011 - 10) {
    	//if(employees().getHireYear() <= 2011 - 10) {
    			employees[i].print();
				//employees().print();
    	}
  }
    	System.out.println("\nEmployees with salary greater than 100000.00.");
    for(int i = 0; i < employees.length; i++ ) {
    	if(employees[i].getSalary() > 100000.00)
    	//if(employees(i).getSalary() > 100000.00)
    			employees[i].print();
    		//employees(i).print();
    }
    	System.out.println("\nManagers");
    for(int i = 0; i < managers.length; i++) {
    	if(managers[i].getLeTitle() >= 4) {
    		managers[i].print();
    	}
    }
    	System.out.println("\nAll Employees");
    for(int i = 0; i < employees.length; i++) {
    	employees[i].print();
    }
    	
    	System.out.println("\nAll Managers");
    for(int i = 0; i < managers.length; i++) {
    	managers[i].print();
    }
    }

  public static class Employee {
    private int id;
    private String name;
    private int hireYear;
    private double salary;
    private int title;

    public Employee() {
      id = 1;
      name = "Nathan Kreider";
      hireYear = 2000;
      salary = 50000.00;
      title = 5;
    }

    public Employee(int newId, String newName, int newHireYear, double newSalary, int newTitle) {
      id = newId;
      name = newName;
      hireYear = newHireYear;
      salary = newSalary;
      title = newTitle;
    }

    public void print() {
      System.out.println("    [" + id + "] Name: " + name + " | Title: " + getTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);
    }
    
    public void setHireYear(int hire1) {
	    hireYear = hire1;
	}
	
	public int getHireYear() {
	    return hireYear;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public String getTitle() {
	  String Title = "";
	  
		if(title == 1) {
			Title = "Trainee";
		} if(title == 2) {
			Title = "Employee";
		} if(title == 3) {
			Title = "Senior Employee";
		} if(title == 4) {
			Title = "Manager";
		} if(title == 5) {
			Title = "Owner";
		}
		
		return Title;
	}
	
	public int getLeTitle() {
		return title;
	}
  }
  
  public static class Manager {
  	private int id;
    private String name;
    private int hireYear;
    private double salary;
    private int title;

    public Manager() {
      id = 1;
      name = "Big Boss Man";
      hireYear = 2000;
      salary = 50000.00;
      title = 5;
    }

    public Manager(int newId, String newName, int newHireYear, double newSalary, int newTitle) {
      id = newId;
      name = newName;
      hireYear = newHireYear;
      salary = newSalary;
      title = newTitle;
    }
    
    public void print() {
      System.out.println("    [" + id + "] Name: " + name + " | Title: " + getMTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);
    }
    
    public String getMTitle() {
	  String Title = "";
	  
		if(title == 4) {
			Title = "Manager";
		} if(title == 5) {
			Title = "Owner";
		}
		
		return Title;
	}
	
	public int getLeTitle() {
		return title;
	}
  	
  }
}

EDIT: Also is it possible to setup the array starter without having to define the amount of array objects?

This post has been edited by Vampiricx3: 16 December 2011 - 10:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Output data to .txt file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Output data to .txt file

Posted 16 December 2011 - 10:23 PM

View PostVampiricx3, on 17 December 2011 - 12:00 AM, said:

Also is it possible to setup the array starter without having to define the amount of array objects?

You will need an ArrayList for that

As far as the file is concerned
http://www.dreaminco...a-file-in-java/
Was This Post Helpful? 1
  • +
  • -

#3 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 16 December 2011 - 11:13 PM

For some reason I can't implement that output code into my java source. T_T
and would the ArrayList just be
Employee employees = new ArrayList();

This post has been edited by Vampiricx3: 16 December 2011 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 04:50 AM

Is anyone able to tell me what I'm doing wrong here?
import java.util.*;
import java.io.*;

public class Working {

  public static void main(String[] args) {
    String read;
    Employee[] employees = new Employee[6];
    Manager[] managers = new Manager[3];
    //ArrayList employees = new ArrayList();
    	employees[0] = new Employee(1, "Taylah Jayne", 2000, 50000.00, 1);
    	employees[1] = new Employee(2, "Hurp", 1981, 1000000.00, 3);
    	employees[2] = new Employee(3, "Hurp Durp", 2011, 10000.00, 1);
    	employees[3] = new Employee(4, "4chan", 2004, 0.00, 2);
    	employees[4] = new Employee(5, "m00t", 2004, 200000.00, 3);
    	employees[5] = new Employee(6, "Derpina", 2009, 15000.00, 1);
    	
    	managers[0] = new Manager(1, "Nathan Kreider", 2000, 500000.00, 5);
    	managers[1] = new Manager(2, "Durp", 2000, 50000.00, 4);
    	managers[2] = new Manager(3, "Durp Hurp", 1821, 180912.00, 5);
    		//employees.add(1, "Nathan Kreider", 2000, 500000.00, 5);
    	
    	try {
             FileWriter out = new FileWriter(new File("C:\\output.txt"));
    	out.write("Employees who have been working here for 10 years or more.");
    for(int i = 0; i < employees.length; i++) {
    	if(employees[i].getHireYear() <= 2011 - 10) {
    	//if(employees().getHireYear() <= 2011 - 10) {
    			out.write("employees[i].print()");
				//employees().print();
    	}
  }
    	out.write("\nEmployees with salary greater than 100000.00.");
    for(int i = 0; i < employees.length; i++ ) {
    	if(employees[i].getSalary() > 100000.00)
    	//if(employees(i).getSalary() > 100000.00)
    			out.write("employees[i].print()");
    		//employees(i).print();
    }
    	out.write("\nManagers");
    for(int i = 0; i < managers.length; i++) {
    	if(managers[i].getLeTitle() >= 4) {
    		out.write("managers[i].print()");
    	}
    }
    	out.write("\nAll Employees");
    for(int i = 0; i < employees.length; i++) {
    	out.write("employees[i].print()");
    }
    	
    	out.write("\nAll Managers");
    for(int i = 0; i < managers.length; i++) {
    	out.write("managers[i].print()");
    }
    	} catch(IOException e) {
            System.out.println("There was a problem:" + e);
        }
    }

  public static class Employee {
    private int id;
    private String name;
    private int hireYear;
    private double salary;
    private int title;

    public Employee() {
      id = 1;
      name = "Nathan Kreider";
      hireYear = 2000;
      salary = 50000.00;
      title = 5;
    }

    public Employee(int newId, String newName, int newHireYear, double newSalary, int newTitle) {
      id = newId;
      name = newName;
      hireYear = newHireYear;
      salary = newSalary;
      title = newTitle;
    }

    public void print() {
      System.out.println("    [" + id + "] Name: " + name + " | Title: " + getTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);
    }
    
    public void setHireYear(int hire1) {
	    hireYear = hire1;
	}
	
	public int getHireYear() {
	    return hireYear;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public String getTitle() {
	  String Title = "";
	  
		if(title == 1) {
			Title = "Trainee";
		} if(title == 2) {
			Title = "Employee";
		} if(title == 3) {
			Title = "Senior Employee";
		} if(title == 4) {
			Title = "Manager";
		} if(title == 5) {
			Title = "Owner";
		}
		
		return Title;
	}
	
	public int getLeTitle() {
		return title;
	}
  }
  
  public static class Manager {
  	private int id;
    private String name;
    private int hireYear;
    private double salary;
    private int title;

    public Manager() {
      id = 1;
      name = "Big Boss Man";
      hireYear = 2000;
      salary = 50000.00;
      title = 5;
    }

    public Manager(int newId, String newName, int newHireYear, double newSalary, int newTitle) {
      id = newId;
      name = newName;
      hireYear = newHireYear;
      salary = newSalary;
      title = newTitle;
    }
    
    public void print() {
      System.out.println("    [" + id + "] Name: " + name + " | Title: " + getMTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);
    }
    
    public String getMTitle() {
	  String Title = "";
	  
		if(title == 4) {
			Title = "Manager";
		} if(title == 5) {
			Title = "Owner";
		}
		
		return Title;
	}
	
	public int getLeTitle() {
		return title;
	} 	
  }
}

It doesn't output to the text file.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2698
  • View blog
  • Posts: 11,366
  • Joined: 20-September 08

Re: Output data to .txt file

Posted 17 December 2011 - 04:58 AM

a. you don't close the FileWriter so it will write nothing at all
b. when you do close it, you're writing String literals instead of data, which isn't useful
c. you should have a toString method in your objects ( out.println(objects[i]); )
d. you should use a PrintWriter or you'll create problems for yourself with linefeeds
Was This Post Helpful? 0
  • +
  • -

#6 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 07:13 AM

okay, I added a
out.close();

it outputted the data, except it doesn't output the data that should be output from the
employee[i].print

it outputs the code directly. the code i'm using to output is
out.write("employees[i].print()");

if I take out the quotation marks, i just get void errors. any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2698
  • View blog
  • Posts: 11,366
  • Joined: 20-September 08

Re: Output data to .txt file

Posted 17 December 2011 - 07:51 AM

Quote

it outputted the data, except it doesn't output the data


I explained that's what it would do in b.above

Quote

any suggestions?


I made those in c. and d. above. If you get your class hierarchy correct, you should only have to implement toString in one place. Hint: a Manager is also an Employee (which probably should be an abstract class)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Output data to .txt file

Posted 17 December 2011 - 10:02 AM

Since when in Java do you
Class object = new OtherClass(); ? :)
Employee employees = new ArrayList();

should be
ArrayList<Employee> list = new ArrayList<Employee>();


This post has been edited by pbl: 17 December 2011 - 10:02 AM

Was This Post Helpful? 1
  • +
  • -

#9 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 12:39 PM

View Postg00se, on 17 December 2011 - 07:51 AM, said:

Quote

it outputted the data, except it doesn't output the data


I explained that's what it would do in b.above

Quote

any suggestions?


I made those in c. and d. above. If you get your class hierarchy correct, you should only have to implement toString in one place. Hint: a Manager is also an Employee (which probably should be an abstract class)

So scrap the entire Manager class? And make the employee class an abstract class?
Sorry for all the questions, Java is my super weak point, trying to learn some of the stuff that didnt make sense in class.
And how would I setup a toString?

View Postpbl, on 17 December 2011 - 10:02 AM, said:

Since when in Java do you
Class object = new OtherClass(); ? :)
Employee employees = new ArrayList();

should be
ArrayList<Employee> list = new ArrayList<Employee>();


Touche, should have thought that one through. Still add objects the same way?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2698
  • View blog
  • Posts: 11,366
  • Joined: 20-September 08

Re: Output data to .txt file

Posted 17 December 2011 - 12:45 PM

You probably need to read something like this

http://www.javabegin...s-and-interface
Was This Post Helpful? 1
  • +
  • -

#11 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 12:50 PM

I'll have a read, as its 3:48am and I'm doing this on my Android phone. Cheers man. Hopefully this helps me.
Was This Post Helpful? 0
  • +
  • -

#12 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 06:41 PM

Okay, I changed the class to an abstract instead of a static. Now i'm getting errors from ALL of my array objects saying
error: non-static variable this cannot be referenced from a static context

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Output data to .txt file

Posted 17 December 2011 - 06:45 PM

Don't see why you would need an abstract class, anyhow post your updated code

This post has been edited by pbl: 17 December 2011 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#14 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 07:00 PM

I fucked the manager class off, and killed the quotes within the out.write();, now i'm getting owned with errors.
import java.util.*;
import java.io.*;

public class Working {

  public static void main(String[] args) {
    String read;
    Employee[] employees = new Employee[9];
    //ArrayList employees = new ArrayList();
    	employees[0] = new Employee(1, "Taylah Jayne", 2000, 50000.00, 5);
    	employees[1] = new Employee(2, "Hurp", 1981, 1000000.00, 3);
    	employees[2] = new Employee(3, "Hurp Durp", 2011, 10000.00, 1);
    	employees[3] = new Employee(4, "4chan", 2004, 0.00, 2);
    	employees[4] = new Employee(5, "m00t", 2004, 200000.00, 3);
    	employees[5] = new Employee(6, "Derpina", 2009, 15000.00, 1);
    	employees[6] = new Employee(7, "Nathan Kreider", 2000, 500000.00, 5);
    	employees[7] = new Employee(8, "Durp", 2000, 50000.00, 4);
    	employees[8] = new Employee(9, "Durp Hurp", 1821, 180912.00, 5);
    		//employees.add(1, "Nathan Kreider", 2000, 500000.00, 5);
  
    	try {
             FileWriter out = new FileWriter(new File("C:\\output.txt"));
    	out.write("Employees who have been working here for 10 years or more.");
    for(int i = 0; i < employees.length; i++) {
    	if(employees[i].getHireYear() <= 2011 - 10) {
    	//if(employees().getHireYear() <= 2011 - 10) {
    			out.write(employees[i].print());
				//employees().print();
    	}
  }
    	out.write("\nEmployees with salary greater than 100000.00.");
    for(int i = 0; i < employees.length; i++ ) {
    	if(employees[i].getSalary() > 100000.00)
    	//if(employees(i).getSalary() > 100000.00)
    			out.write(employees[i].print());
    		//employees(i).print();
    }
    	out.write("\nManagers");
    for(int i = 0; i < employees.length; i++) {
    	if(employees[i].getLeTitle() >= 4) {
    		out.write(employees[i].print());
    	}
    }
    	out.write("\nAll Employees");
    for(int i = 0; i < employees.length; i++) {
    	out.write(employees[i].print());
    }
    	out.close();
    	} catch(IOException e) {
            System.out.println("There was a problem:" + e);
        }
    }

  public abstract class Employee {
    private int id;
    private String name;
    private int hireYear;
    private double salary;
    private int title;

    public Employee() {
      id = 1;
      name = "Nathan Kreider";
      hireYear = 2000;
      salary = 50000.00;
      title = 5;
    }

    public Employee(int newId, String newName, int newHireYear, double newSalary, int newTitle) {
      id = newId;
      name = newName;
      hireYear = newHireYear;
      salary = newSalary;
      title = newTitle;
    }

    public void print() {
      System.out.println("    [" + id + "] Name: " + name + " | Title: " + getTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);
    }
    
    public void setHireYear(int hire1) {
	    hireYear = hire1;
	}
	
	public int getHireYear() {
	    return hireYear;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public String getTitle() {
	  String Title = "";
	  
		if(title == 1) {
			Title = "Trainee";
		} if(title == 2) {
			Title = "Employee";
		} if(title == 3) {
			Title = "Senior Employee";
		} if(title == 4) {
			Title = "Manager";
		} if(title == 5) {
			Title = "Owner";
		}
		
		return Title;
	}
	
	public int getLeTitle() {
		return title;
	}
  }
}

errors are..
C:\Users\Nathan\Documents\Working.java:10: error: non-static variable this cannot be referenced from a static context
        employees[0] = new Employee(1, "Taylah Jayne", 2000, 50000.00, 5);
                       ^
C:\Users\Nathan\Documents\Working.java:11: error: non-static variable this cannot be referenced from a static context
        employees[1] = new Employee(2, "Hurp", 1981, 1000000.00, 3);
                       ^
C:\Users\Nathan\Documents\Working.java:12: error: non-static variable this cannot be referenced from a static context
        employees[2] = new Employee(3, "Hurp Durp", 2011, 10000.00, 1);
                       ^
C:\Users\Nathan\Documents\Working.java:13: error: non-static variable this cannot be referenced from a static context
        employees[3] = new Employee(4, "4chan", 2004, 0.00, 2);
                       ^
C:\Users\Nathan\Documents\Working.java:14: error: non-static variable this cannot be referenced from a static context
        employees[4] = new Employee(5, "m00t", 2004, 200000.00, 3);

                       ^
C:\Users\Nathan\Documents\Working.java:15: error: non-static variable this cannot be referenced from a static context
        employees[5] = new Employee(6, "Derpina", 2009, 15000.00, 1);
                       ^
C:\Users\Nathan\Documents\Working.java:16: error: non-static variable this cannot be referenced from a static context
        employees[6] = new Employee(7, "Nathan Kreider", 2000, 500000.00, 5);
                       ^
C:\Users\Nathan\Documents\Working.java:17: error: non-static variable this cannot be referenced from a static context
        employees[7] = new Employee(8, "Durp", 2000, 50000.00, 4);
                       ^
C:\Users\Nathan\Documents\Working.java:18: error: non-static variable this cannot be referenced from a static context
        employees[8] = new Employee(9, "Durp Hurp", 1821, 180912.00, 5);
                       ^
C:\Users\Nathan\Documents\Working.java:27: error: 'void' type not allowed here
                out.write(employees[i].print());
                                            ^
C:\Users\Nathan\Documents\Working.java:35: error: 'void' type not allowed here
                out.write(employees[i].print());
                                            ^
C:\Users\Nathan\Documents\Working.java:41: error: 'void' type not allowed here
            out.write(employees[i].print());
                                        ^
C:\Users\Nathan\Documents\Working.java:46: error: 'void' type not allowed here
        out.write(employees[i].print());
                                    ^
13 errors

Was This Post Helpful? 0
  • +
  • -

#15 Vampiricx3  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 85
  • Joined: 01-December 11

Re: Output data to .txt file

Posted 17 December 2011 - 07:12 PM

never mind. programmer from a different forum fixed this for me.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2