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