Question:
I NEED SOME SERIOUS HELP IN JAVA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOW!!!!!!!!!!!?
freestyledesign2
2011-01-27 17:03:01 UTC
Apparently people have NO idea what an array list is or how to find elements in an array. This is my third time posting, and this assignment is due tonight.

I have these classes: Employee, Payroll, PayrollTest(main method)

Employee has gettors and settors and fields for first name, last name, address, salary.

Payroll has an array list for Employee and takes data from a file filed with employees

In payroll I am supposed to find a specific employee by entering in the name of the employee and if found print out all of the employees info.

I have no idea how to search an arraylist. I NEED SOME HELP!
Four answers:
anonymous
2011-01-27 17:08:49 UTC
UPDATE: I have appended a solution for you using the code you presented. Note that if you found it difficult to follow these answers and references, then you might want to reconsider your study of programming languages.



A few minutes with google turned up the following which is a great example of search through an arraylist of objects to find one that meets a certain criteria.



public void search(String title)

{

int index = 0;

boolean found = false;

Item item;

while(index < items.size() && !found) {

//String title = (index);

item = items.get(index);

if (item.gettheTitle().equals(title)) {

found = true;

System.out.println("Media Title: " + title);

}

else {

index++;

System.out.println("Search term not found");

}

}



This should help. Good luck.



--------- UPDATE -- Using your code.

Contents of 'emp.txt'

Joe~Smith~1 Lane Rd~500.00

Stan~LeMan~2 Lane Rd~600.00



Contents of Employee.java:

public class Employee {

public String f;

public String l;

public String a;

public double s;

Employee(String fname, String lname, String addr, double salary) {

f = fname;

l = lname;

a = addr;

s = salary;

}

public String getFname() { return f; }

public String getLname() { return l; }

public String getAddr() { return a; }

public double getSalary() { return s; }

}



Contents of Payroll.java:

import java.util.*;

import java.io.*;



public class Payroll

{

private ArrayList payList;



static public void main(String[] args) {

Payroll pr = new Payroll("emp.txt");

pr.findAllInfo("Missy","Missing");

pr.findAllInfo("Stan","LeMan");

}



public Payroll()

{

payList = new ArrayList();

}



public Payroll(String infilename)

{

payList = new ArrayList();

Scanner input = null;

try

{

input = new Scanner(new File(infilename));

while(input.hasNextLine())

{

String line = input.nextLine();

StringTokenizer str = new StringTokenizer(line, "~");

String f = str.nextToken();

String l = str.nextToken();

String a = str.nextToken();

String s = str.nextToken();

payList.add(new Employee(f, l, a, Double.parseDouble(s)));

}

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

finally

{



if(input != null)

{

input.close();

}

}

}



public boolean add(String fname, String lname, String addr,

double salary)

{

if(payList.add(new Employee(fname, lname, addr, salary)) == true)

{

return true;

}

else

{

return false;

}

}



public boolean remove(String fname, String lname)

{



return true;

}



public void printPayroll()

{

int i;

for (i=0; i < payList.size(); i++)

{

System.out.println(payList.get(i));

}

}



public boolean findAllInfo(String fname, String lname)

{

Employee ans = null;

System.out.println("Searching for: " + fname + " " + lname);

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

{

Employee emp = payList.get(i);

System.out.println("Checking: " + emp.getFname() + " " + emp.getLname());

if (emp.getLname().equals(lname)

&& emp.getFname().equals(fname)) {

System.out.println("Found:");

System.out.println("Lname: "+emp.getLname());

System.out.println("Fname: "+emp.getFname());

System.out.println("Addr: "+emp.getAddr());

System.out.println("Salary: "+emp.getSalary());

ans = emp;

break;

}

}

if (ans == null) {

System.out.println("Not Found!");

}

return ans != null;

}

}





Execution of 'java Payroll' after compiling:

Searching for: Missy Missing

Checking: Joe Smith

Checking: Stan LeMan

Not Found!

Searching for: Stan LeMan

Checking: Joe Smith

Checking: Stan LeMan

Found:

Lname: LeMan

Fname: Stan

Addr: 2 Lane Rd

Salary: 600.0
ultimate reality1
2011-01-28 01:18:05 UTC
Searching an arrayList of objects works just like searching through an array of objects. You can do a for each loop which searches all the objects in your array list (no need to specify an ending condition)



for (Employee e: thearrayList)

{

if (e.getName().equals("name here"))

{

//print out all his info here or send the object e to a method specifically for printing info



break; //can just exit the loop as you found him

}



}//end loop



That's basically one way of doing this.
?
2011-01-28 01:20:27 UTC
Well not everything is wrote for you.. you expect some high level function just to do a loop and compare each of the names?



Something like this..



Employee FindEmployee(Employee[] employees, string name)

{

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

{

if (employees[i].Name == name)

{

return employee[i];

}

}

return null;

}
BADE PAPA
2011-01-28 01:25:06 UTC
u need to iterate the arraylist....

here's the sample code:



for (Iterator iter = EmployeeInfoList.iterator(); iter.hasNext();) {

Employee emp= (Employee ) iter.next();

String empName = emp.getName();

if (empName != ""&& (empName.equals("XYZ") || empName == "XYZ")) {

request.setAttribute("emp",emp);

break;

}







Hopeing this will help you.. :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...