Read into array from .txt

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4045 Views - Last Post: 15 January 2012 - 10:41 PM Rate Topic: -----

#1 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Read into array from .txt

Posted 15 January 2012 - 02:26 AM

Hey guys, I come to you today with a question. How would I read data (objects?) into an arraylist from a .txt file? I've got an output that outputs just the raw data from each array, but I just don't exactly know how to read it in properly, if anyone can link me to a tutorial/snippet that would be great, but if someone can help me with this, that would be even better. My source code is below.
import java.io.*;

import java.util.*; 



public class Working {



	public Working() {

		String read;

	    //Employee[] employees = new Employee[10];

	    ArrayList<Employee> employees = new ArrayList<Employee>();

	    

	    	employees.add(new Employee(1, "Taylah Jayne", 2000, 50000.00, 5));

	    	employees.add(new Employee(2, "Hurp Hurp", 1981, 1000000.00, 3));

	    	employees.add(new Employee(3, "Hurp Durp", 2011, 10000.00, 1));

	    	employees.add(new Employee(4, "4chan l00lz", 2004, 0.00, 2));

	    	employees.add(new Employee(5, "m00t harz", 2004, 200000.00, 3));

	    	employees.add(new Employee(6, "Derpina z0r", 2009, 15000.00, 1));

	    	employees.add(new Employee(7, "Nathan Kreider", 2000, 500000.00, 5));

	    	employees.add(new Employee(8, "Durp Dur", 2000, 50000.00, 4));

	    	employees.add(new Employee(9, "Durp Hurp", 1821, 180912.00, 5));

	    	employees.add(new Employee(10, "Le Durp", 1921, 1920101.00, 2));

	    	employees.add(new Employee(11, "Nathan Kreider", 2000, 500000.00, 5));

	    	employees.add(new Employee(12, "What Durp", 1200, 500000.00, 1));

	    	employees.add(new Employee(13, "Oh No", 1800, 120000.00, 3));

	    	employees.add(new Employee(14, "Waddup Bruh", 2012, 180000.00, 1));

	    	employees.add(new Employee(15, "Jai Mason", 2007, 12000.00, 3));
		employees.add(new Employee(16, "Pan Pacific", 2001, 1000000.00, 4));

	    	

	    	try {

	             FileWriter out = new FileWriter(new File("src/main.txt"));

	             FileWriter trainee = new FileWriter(new File("src/trainee.txt"));

	             FileWriter manager = new FileWriter(new File("src/manager.txt"));

	             

	             FileWriter cOut = new FileWriter(new File("src/c++/cOut.txt"));

	             FileWriter cTrainee = new FileWriter(new File("src/c++/cTrainee.txt"));

	             FileWriter cManager = new FileWriter(new File("src/c++/cManager.txt"));

	    

	        out.write("All Employees");

	    	System.out.println("All Employees");

	    for(int i = 0; i < employees.size(); i++) {

	    	out.write("\n" + employees.get(i).getData());

	    	Out.write(employees.get(i).cOutput() + "\n");

	    			  employees.get(i).print();

	    }

	    	out.write("\n\nEmployees who have been working here for 10 years or more.");

	    	System.out.println("\nEmployees who have been working here for 10 years or more.");

	    for(int i = 0; i < employees.size(); i++) {

	    	if(employees.get(i).getHireYear() <= 2011 - 10) {

	    	//if(employees().getHireYear() <= 2011 - 10) {

	    			out.write("\n" + employees.get(i).getData());

	    					  employees.get(i).print();

					//employees().print();

	    	}

	  }

	    	out.write("\n\nEmployees with salary greater than 100000.00.");

	    	System.out.println("\nEmployees with salary greater than 100000.00.");

	    for(int i = 0; i < employees.size(); i++ ) {

	    	if(employees.get(i).getSalary() > 100000.00)

	    	//if(employees(i).getSalary() > 100000.00)

	    			out.write("\n" + employees.get(i).getData());

	    					  employees.get(i).print();

	    		//employees(i).print();

	    }

	    	System.out.println("\nManagers");

	    for(int i = 0; i < employees.size(); i++) {

	    	if(employees.get(i).getLeTitle() >= 4) {

	    		manager.write("\n" + employees.get(i).getData());

	    		cManager.write(employees.get(i).cOutput() + "\n");

	    				  employees.get(i).print();

	    	}

	    }

	    	out.write("\n\nTrainee's");

	    	System.out.println("\nTrainees");

	    for(int i = 0; i < employees.size(); i++) {

	    	if(employees.get(i).getLeTitle() == 1) {

	    		out.write("\n" + employees.get(i).getData());

	    		trainee.write("\n" + employees.get(i).getData());

	    		cTrainee.write(employees.get(i).cOutput() + "\n");

	    				  employees.get(i).print();

	    	}

	    }

	    	cManager.close();

	    	cTrainee.close();

	    	cOut.close();

	    	

	    	manager.close();

	 		trainee.close();

			out.close();

	    	} catch(IOException e) {

	            System.out.println("There was a problem:" + e);

	        }

	}



  public static void main(String[] args) {

    new Working();

    }



  public 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 String getData() {

      return "  [" + id + "] Name: " + name + " | Title: " + getTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary;

    }

    

    public void print() {

    	System.out.println("   [" + id + "] Name: " + name + " | Title: " + getTitle() + " | Hired in: " + hireYear + " | Salary: $" + salary);

    }

    

    public String cOutput() {

    	return id + " " + name + " " + getTitle() + " " + hireYear + " " + salary;

    }

    

    public void setHireYear(int hire1) {

	    hireYear = hire1;

	}

	

	public int getHireYear() {

	    return hireYear;

	}

	

	public double getSalary() {

		return salary;

	}

	

	public String getTitle() {

	  String Title = "";

	  

		if(getHireYear() > 2010) {

			Title = "Trainee";

		} if(title == 1) {

			Title = "Trainee";

		} if(title == 2) {

			Title = "Employee";

		} if(title == 3) {

			Title = "Senior";

		} if(title == 4) {

			Title = "Manager";

		} if(title == 5) {

			Title = "Owner";

		}

		

		return Title;

	}

	

	public int getLeTitle() {

		return title;

	}

  }

}


The whole
cOut.write(employees.get(i).cOutput() + "\n");

along with
public String cOutput() {

   return id + " " + name + " " + getTitle() + " " + hireYear + " " + salary;

}

prints out the "raw" data from the Arraylist. Thank you in advance, any helpers will receive +1 rep.

Is This A Good Question/Topic? 0
  • +

Replies To: Read into array from .txt

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 03:13 AM

Quote

How would I read data (objects?) into an arraylist from a .txt file?


Why would you not post an example of the text file for a question like this!?
Was This Post Helpful? 0
  • +
  • -

#3 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Re: Read into array from .txt

Posted 15 January 2012 - 05:04 AM

1 Taylah Jayne 5 2000 50000.0
2 Hurp Hurp 3 1981 1000000.0
3 Hurp Durp 1 2011 10000.0
4 4chan l00lz 2 2004 0.0
5 m00t harz 3 2004 200000.0
6 Derpina z0r 1 2009 15000.0
7 Nathan Kreider 5 2000 500000.0
8 Durp Dur 4 2000 50000.0
9 Durp Hurp 5 1821 180912.0
10 Le Durp 2 1921 1920101.0
11 Nathan Kreider 5 2000 500000.0
12 What Durp 1 1200 500000.0
13 Oh No 3 1800 120000.0
14 Waddup Bruh 1 2012 180000.0
15 Jai Mason 3 2007 12000.0

Although you could easily see how it would be output to the file by looking at the code...

This post has been edited by Vampiricx3: 15 January 2012 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 05:14 AM

Use a Scanner:


while(s.hasNext()) {
   int id = s.nextInt();
   String fname = s.next();
   String lname = s.next();
   String title = s.next();
   int hireYear = s.nextInt();
   double salary = s.nextDouble();
}



That might not be strictly accurate since the file you posted doesn't tie in with the code you wrote afaics

This post has been edited by g00se: 15 January 2012 - 05:17 AM
Reason for edit:: mismatch

Was This Post Helpful? 1
  • +
  • -

#5 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Re: Read into array from .txt

Posted 15 January 2012 - 05:17 AM

Yeah, I have no idea on how to implement this.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 05:20 AM

Getting the Scanner would be


Scanner s = new Scanner(new File("input.txt"));
// Use it
s.close();

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Read into array from .txt

Posted 15 January 2012 - 05:22 AM

Or read each line from the file as a single string and then split() it into the desired parts.
Was This Post Helpful? 0
  • +
  • -

#8 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Re: Read into array from .txt

Posted 15 January 2012 - 05:31 AM

		Scanner s = new Scanner(new File("src/c++/cOut.txt"));
			while(s.hasNext()) {
   				int id = s.nextInt();
			        String fname = s.next();
   				String lname = s.next();
   				String title = s.next();
   				int hireYear = s.nextInt();
   				double salary = s.nextDouble();
			}

		employees.add(new Employee(s));
		s.close();

pretty much what I tried, pretty much how I failed.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 05:33 AM

Quote

pretty much what I tried, pretty much how I failed.


http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#10 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Re: Read into array from .txt

Posted 15 January 2012 - 05:36 AM

nathan@ubuntu:~/Desktop/Employee [Java]$ javac Working.java
Working.java:20: cannot find symbol
symbol  : constructor Employee(java.util.Scanner)
location: class Working.Employee
		employees.add(new Employee(s));
		              ^
1 error

my errors.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 05:38 AM

You're trying to create an Employee by passing it a reference to the Scanner for some reason. You should be passing in the fields you read. If the text file you posted is accurate then it will have to be read:


        while (s.hasNext()) {
            int id = s.nextInt();
            String fname = s.next();
            String lname = s.next();
            int something = s.nextInt();
            int hireYear = s.nextInt();
            double salary = s.nextDouble();
        }


Was This Post Helpful? 1
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Read into array from .txt

Posted 15 January 2012 - 05:40 AM

Can you not see the error?
Was This Post Helpful? 1
  • +
  • -

#13 Vampiricx3  Icon User is offline

  • D.I.C Head

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

Re: Read into array from .txt

Posted 15 January 2012 - 05:48 AM

Yeah, I don't know. I'm just trying to get ready for my programming course coming up. None of this is really helping.
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Read into array from .txt

Posted 15 January 2012 - 05:58 AM

Quote

employees.add(new Employee(1, "Taylah Jayne", 2000, 50000.00, 5));



Who wrote that - you or someone else?
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: Read into array from .txt

Posted 15 January 2012 - 07:36 AM

Someone on this forum gave me hints on the whole "ArrayList" thing.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2