0

I am doing an assignment where we have to use an Array List to make an application that store employee info (salary, name, ID #, start date). We were told to create a class for teh records of the employees. I have to make the program remove an entire entry from just the employee ID # which I'm having a hard time figuring out.

//removes entries from record using only ID
    String id;
    int i;
    id = idIn.getText();

below is the code for the creation of the class

  //class for employees
class employees{
    String first, last, id, date, salary;

    employees (String _first, String _last, String _id, String _date, String _salary){
        first = _first;
        last = _last;
        id = _id;
        date = _date;
        salary = _salary;

    }
}
    System.out.println(id);
    i = records.indexOf(id);
    System.out.println(i);

This is the creation of the Array list

//create array for records
ArrayList <employees> records = new ArrayList <employees>();

And this is how entries are added to the list

//add records to array

   employees e;
   String first, last, id, date, salary;
    first = firstIn.getText();
    last = lastIn.getText();
    id = idIn.getText();
    date = dateIn.getText();
    salary = salaryIn.getText();

    e = new employees (first, last, id, date, salary);
    records.add(e);

I get a value of -1 when searching for the index of the ID #

6
  • My online teacher told me this "When you had created your class for your employee object you might have included an integer parameter for the ID. You will have to search through each record (basically each row) to find the correct ID. You would use a for loop to go through each employee from 0 to size-1. If you find the ID then the number that you are at for the loop represents the index value of the employee within the arrayList. You can then remove that." but I' not really sure what he means, also my ID # is a string.... Commented Jun 18, 2017 at 16:19
  • This means ID is not in records, or the ID inside records is not of type String. Commented Jun 18, 2017 at 16:19
  • please show the code for the Array List construction. Commented Jun 18, 2017 at 16:19
  • 1
    If records is your ArrayList, then that's not how you search for an item with that ID. You have to iterate through the list using an iterator, check the id, and then delete (or do it indirectly using streams). Commented Jun 18, 2017 at 16:19
  • 1
    myList.removeIf(x -> x.getID() == someID); Commented Jun 18, 2017 at 16:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.