Client making a choose...(Linking Problem)
I have made a scanner in which it will ask a client to make a selection.
I'm having troubles on choose 2 thou. I can not figure out how to make it work. Choose 2 is calledpublic void findPerson()
Any help would be great..
class PersonClass{
private String empid;
private String lname;
private String fname;
private String street;
private String city;
private String state;
private String zip;
privatedouble payrate;
privateint yearsworked;
public PersonClass(String id){
empid = id;
}
public PersonClass(String id, String ln, String fn, String st, String ct, String se, String zp,double pr,int yw){
empid = id;
lname = ln;
fname = fn;
street = st;
city = ct;
state = se;
zip = zp;
payrate = pr;
yearsworked = yw;
}
// accessors
public String getID(){return empid;}
public String getFname(){return fname;}
public String getLname(){return lname;}
public String getStree(){return street;}
public String getCity(){return city;}
public String getState(){return state;}
public String getZip(){return zip;}
publicdouble getPayrate(){return payrate;}
publicint getYearsworked(){return yearsworked;}
}
publicclass EmployeeData{
static ArrayList<PersonClass> arlist;
static Scanner kbd;
publicstatic PersonClass makePerson(){
}
publicvoid findPerson(){
String id_flag ="";
Scanner input_flag =new Scanner(System.in);
System.out.println("Enter your info please ie: AB1234: ");
id_flag = input_flag.next();
boolean notfound =true;
for (PersonClass e : arlist){
String emp = e.getID();
if (emp.equals(id_flag)){
System.out.println("Hello" + ("e.lname"));
notfound =false;
}
}
if (notfound ==true){
System.out.println("Error - Employee not found");
// back to menu?
}
}//close findperson
publicstaticvoid main(String[] args){
// make array list object
arlist =new ArrayList<PersonClass>();
// make a scanner
kbd =new Scanner(System.in);
int choice;
System.out.println("Make a Section: ");
System.out.println("1. Enter Employees ");
System.out.println("2. Find Employees ");
System.out.println("3. Exit this Program ");
System.out.print("\nPlease press Enter afer each response");
System.out.println("Enter your chose please: ");
choice = kbd.nextInt();
kbd.nextLine();
if (choice == 1){// if 1 is select go to makePerson
}//close while loop
if (choice == 2){// if 2 is select go to find
EmployeeData.findPerson();
}//close while loop
}// close the choice==2
if (choice == 3){
}// close the choice == 3
// print out all elements of array list
for (PersonClass idx : arlist){
System.out.printf("Employee Id is %n", idx.getID());
System.out.printf("Name is %s - %s%n", idx.getFname(),idx.getLname());
System.out.printf("Street is %s%n", idx.getStree());
System.out.printf("City is %s%n", idx.getCity());
System.out.printf("State is %s%n", idx.getState());
System.out.printf("Zip Code is %s%n", idx.getZip());
System.out.printf("Payrate is %8.2f%n", idx.getPayrate());
System.out.printf("Years worked are %d\n", idx.getYearsworked());
System.out.println("--");
}
}
}
Thanks
sandr
[7768 byte] By [
SandyReda] at [2007-11-27 3:51:47]

findPerson is not declared static and you are accessing it as EmployeeData.findPerson()
OK. If I make it static I get more errors and I don't know how to sovle or correct the problems. Is there another way around this if so can you please tell me how to correct this...thankssandyR
Ok, the following compiles although I didn't test if it works.
package uk.co.jemos.experiments;
import java.util.ArrayList;
import java.util.Scanner;
class PersonClass {
private String empid;
private String lname;
private String fname;
private String street;
private String city;
private String state;
private String zip;
private double payrate;
private int yearsworked;
public PersonClass(String id) {
empid = id;
}
public PersonClass(String id, String ln, String fn, String st, String ct, String se, String zp, double pr, int yw) {
empid = id;
lname = ln;
fname = fn;
street = st;
city = ct;
state = se;
zip = zp;
payrate = pr;
yearsworked = yw;
}
// accessors
public String getID() {return empid;}
public String getFname() {return fname;}
public String getLname() {return lname;}
public String getStree() {return street;}
public String getCity() {return city;}
public String getState() {return state;}
public String getZip() {return zip;}
public double getPayrate() {return payrate;}
public int getYearsworked() {return yearsworked;}
}
public class EmployeeData {
static ArrayList<PersonClass> arlist;
static Scanner kbd;
public static PersonClass makePerson() {
return null;//You need to implement this
}
public void findPerson() {
String id_flag = "";
Scanner input_flag = new Scanner(System.in);
System.out.println("Enter your info please ie: AB1234: ");
id_flag = input_flag.next();
boolean notfound = true;
for (PersonClass e : arlist) {
String emp = e.getID();
if (emp.equals(id_flag)) {
System.out.println("Hello" + ("e.lname"));
notfound = false;
}
}
if (notfound == true) {
System.out.println("Error - Employee not found");
// back to menu?
}
}//close findperson
public static void main(String[] args) {
EmployeeData main = new EmployeeData();
// make array list object
arlist = new ArrayList<PersonClass>();
// make a scanner
kbd = new Scanner(System.in);
int choice;
System.out.println("Make a Section: ");
System.out.println("1. Enter Employees ");
System.out.println("2. Find Employees ");
System.out.println("3. Exit this Program ");
System.out.print("\nPlease press Enter afer each response");
System.out.println("Enter your chose please: ");
choice = kbd.nextInt();
kbd.nextLine();
if (choice == 1) { // if 1 is select go to makePerson
}//close while loop
if (choice == 2) { // if 2 is select go to find
main.findPerson();
}// close the choice==2
if (choice == 3) {
}// close the choice == 3
// print out all elements of array list
for (PersonClass idx : arlist) {
System.out.printf("Employee Id is %n", idx.getID());
System.out.printf("Name is %s - %s%n", idx.getFname(),idx.getLname());
System.out.printf("Street is %s%n", idx.getStree());
System.out.printf("City is %s%n", idx.getCity());
System.out.printf("State is %s%n", idx.getState());
System.out.printf("Zip Code is %s%n", idx.getZip());
System.out.printf("Payrate is %8.2f%n", idx.getPayrate());
System.out.printf("Years worked are %d\n", idx.getYearsworked());
System.out.println("--");
}
}
}
There were few mistakes. First I created an instance of the EmployeeData class and used that to invoke the findPerson method which now can be declared without the static keyword. Then there was a } too much (the one labelled as end of while loop...Which while loop?); third, the method makePerson declared a return type but didn't return anything. You know, sometimes IDEs like Eclipse or NetBeans can really help ;)
OK I made the correction that you gave me but it will invoke the findPerson method. It should work because I have done this in the past.
I did a debug and got this error.
<terminated>EmployeeData (1) [Java Application]
<disconnected>arrayproject.EmployeeData at localhost:1253
<terminated, exit value: 0>C:\Program Files\EasyEclipse Desktop Java 1.2.1\jre\bin\javaw.exe (May 9, 2007 2:04:22 PM)
I have attached my whole project and if someone can help me that would be great..
class PersonClass {
private String empid;
private String lname;
private String fname;
private String street;
private String city;
private String state;
private String zip;
private double payrate;
private int yearsworked;
public PersonClass(String id) {
empid = id;
}
public PersonClass(String id, String ln, String fn, String st, String ct, String se, String zp, double pr, int yw) {
empid = id;
lname = ln;
fname = fn;
street = st;
city = ct;
state = se;
zip = zp;
payrate = pr;
yearsworked = yw;
}
// accessors
public String getID() {return empid;}
public String getFname() {return fname;}
public String getLname() {return lname;}
public String getStree() {return street;}
public String getCity() {return city;}
public String getState() {return state;}
public String getZip() {return zip;}
public double getPayrate() {return payrate;}
public int getYearsworked() {return yearsworked;}
}
public class EmployeeData {
static ArrayList<PersonClass> arlist;
static Scanner kbd;
public static PersonClass makePerson() {
PersonClass temp = null;
// prompt for data
String id;
String ln;
String fn;
String st;
String se;
String ct;
String zp;
double pr;
int years;
System.out.print("Enter ID Number ==>");
id = kbd.next();
System.out.print("Enter Last Name ==>");
ln = kbd.next();
System.out.print("Enter First Name ==>");
fn = kbd.next();
System.out.print("Enter the address==>");
st = kbd.next();
System.out.print("Enter City ==>");
ct = kbd.next();
System.out.print("Enter State ==>");
se = kbd.next();
System.out.print("Enter Zip ==>");
zp = kbd.next();
System.out.print("Enter payrate as double ==>");
pr = kbd.nextDouble();
System.out.print("Enter years worked ==>");
years = kbd.nextInt();
// make an object
temp = new PersonClass(id, ln,fn,st,ct,se, zp, pr,years);
return temp;
}
public void displayMatch() {
String id_flag = "";
Scanner kbd = new Scanner(System.in);
System.out.println("Enter your info please ie: AB1234: ");
id_flag = kbd.next();
boolean notfound = true;
for (PersonClass e : arlist) {
String emp = e.getID();
if (emp.equals(id_flag)) {
System.out.println("Hello" + ("e.lname"));
notfound = false;
}
}
if (notfound == true) {
System.out.println("Error - Employee not found");
// back to menu?
}
}//close findperson
public static void main(String[] args) {
EmployeeData emp = new EmployeeData();
// make array list object
arlist = new ArrayList<PersonClass>();
// make a scanner
kbd = new Scanner(System.in);
int choice;
System.out.println("Make a Section: ");
System.out.println("1. Enter Employees ");
System.out.println("2. Find Employees ");
System.out.println("3. Exit this Program ");
System.out.print("\nPlease press Enter afer each response");
System.out.println("Enter your chose please: ");
choice = kbd.nextInt();
kbd.nextLine();
if (choice == 1) { // if 1 is select go to makePerson
// create people until select stop
boolean endData = false;
while (!endData) {
PersonClass temp = makePerson();
arlist.add(temp);
System.out.println("Add More employees (Y/N)-->");
String ans = kbd.next();
if (ans.equalsIgnoreCase("N")) {
endData = true;
}
}//close while loop
if (choice == 2) { // if 2 is select go to find
emp.displayMatch();
}// close the choice==2
if (choice == 3) {
System.out.printf("Good bye");
}// close the choice == 3
// print out all elements of array list
for (PersonClass idx : arlist) {
System.out.printf("Employee Id is %n", idx.getID());
System.out.printf("Name is %s - %s%n", idx.getFname(),idx.getLname());
System.out.printf("Street is %s%n", idx.getStree());
System.out.printf("City is %s%n", idx.getCity());
System.out.printf("State is %s%n", idx.getState());
System.out.printf("Zip Code is %s%n", idx.getZip());
System.out.printf("Payrate is %8.2f%n", idx.getPayrate());
System.out.printf("Years worked are %d\n", idx.getYearsworked());
System.out.println("--");
}
}
}
}
Thanks a bunch
sandyR
