For loop problem..

Can someone please look at this for loop and tell if it if will work or not. This is my third project.

I'm using a arraylist which is listed below...

publicvoid 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?

}

Arraylist

publicstaticvoid main(String[] args){

EmployeeData emp =new EmployeeData();

// make array list object

arlist =new ArrayList<PersonClass>();

I also having trouble put data in the arraylist.

Can some one please show me how to do that.

here is my class info

class PersonClass{

private String empid;

private String lname;

private String fname;

private String address;

privatedouble payrate;

privateint yearsworked;

public PersonClass(String id){

empid = id;

}

I have made accessors as well.

Thanks

red

Message was edited by:

Redheadashley

Message was edited by:

Redheadashley

[2751 byte] By [Redheadashleya] at [2007-11-27 4:07:11]
# 1
This may not be the answer you want, but you should try to write code thatyou can test as soon as possible. Then there's no need to ask if it willwork or not, because you can run it and know that immediately.
DrLaszloJamfa at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 2
ok But can you show me how to objects in the arraylist please.thanksred
Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 3

> I also having trouble put data in the arraylist.

import java.util.*;

public class Person {

private String id;

private String name;

public Person(String id, String name) {

this.id = id;

this.name = name;

}

public String toString() {

return "(" + id + ", " + name + ")";

}

public static void main(String[] args) {

//quick list demo

List < Person > persons = new ArrayList < Person > ();

persons.add(new Person("jqp","John Q. Public"));

persons.add(new Person("gwb","George W. Bush"));

System.out.println(persons);

}

}

DrLaszloJamfa at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 4
To my eye, your for loop looks as if it should work just fine.
petes1234a at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 5
thank you very much for helping me.
Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 6
thanks. I guess I will find out if it works after I insert the object in the arraylist.
Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 7

after adding the object to the arraylist I found out my for loop does not work.

Can some one please tell me why.

thanks.

here is my code

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) {

List < PersonClass > persons = new ArrayList < PersonClass > ();

persons.add(new PersonClass("GD123", "George", "Doe", "1234 st", "Fullerton", "CA", "90876", 2, 3000));

persons.add(new PersonClass("AS125", "John", "Jones", "5897 East", "Fullerton", "CA", "90767", 4, 29000));

System.out.println(persons);

EmployeeData emp = new EmployeeData();

// make array list object

arlist = new ArrayList<PersonClass>();

Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 8
Looks like you've added them to the 'persons' list, instead of the 'arlist' list that you're looping over.
BinaryDigita at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 9

thanks for the help...

But I still can having problems invoke the findPerson method.

public class EmployeeData {

static ArrayList<PersonClass> arlist;

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) {

List < PersonClass > arlist = new ArrayList < PersonClass > ();

arlist.add(new PersonClass("GD123", "George", "Doe", "1234 st", "Fullerton", "CA", "90876", 2, 3000));

arlist.add(new PersonClass("AS125", "John", "Jones", "5897 East", "Fullerton", "CA", "90767", 4, 29000));

System.out.println(arlist);

// 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("\nEnter 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

[b] if (choice == 2) { // if 2 is select go to find[/b][b]((EmployeeData) arlist).displayMatch();[/b]}

thanks

red

Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 10
You have 3 different 'arlist' references. Only one is being populated, but then you replace it with a new empty list right after you print it out. The static one is never populated with anything.
BinaryDigita at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 11
I hate to sound dumb here but which 'arlist' references do I get rid of. .I got rid of this one// make array list objectarlist = new ArrayList<PersonClass>();thanksredMessage was edited by: Redheadashley
Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 12
I would also get rid of the other one you created in main, because the displayMatch method can't see it and is actually looping on the empty static one instead. Just use the static one.
BinaryDigita at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...
# 13
I really appricate the help...I'm still having problems with the loop section...I can tell its going to be a very long weekend and next week as well.red6 more days of class and 5 project to solve and one to write still.
Redheadashleya at 2007-7-12 9:12:23 > top of Java-index,Java Essentials,New To Java...