Removing an Object from an Array of Objects in Java -
i working on school project learning use arrays of objects. have come across 2 separate issues along way address. using netbeans ide code mention below.
firstly, after have added values array, attempt use list button should display values in jtextarea. there no noticeable errors pop-up although, values "null" instead of user inputted values should say. since there 5 values program displays "null null null null null". not sure why , input appreciated.
secondly, attempting allow user remove information based on 1 of 5 values have been stored. in case of program user must enter "employee id" remove of said employee's data program list. having trouble doing able see in code below.
so sum things up: how can correct random "null" error imputed data displayed? how can remove values of given index based on user input idnumber?
things keep in mind: - need use array of objects - new coding please excuse possible ignorance - appears missing included in netbeans code , therefore may not have been included, main code of concern in mind
here's code:
//arraylist of objects declared arraylist <employees> list = new arraylist <employees>(); private void addbuttonactionperformed(java.awt.event.actionevent evt) { employees e; //declaring strings string idnumber, firstname, lastname, annualsalary, startdate; //reads input fields idnumber = idinput.gettext(); firstname = fnameinput.gettext(); lastname = lnameinput.gettext(); annualsalary = annualinput.gettext(); startdate = startdateinput.gettext(); //sends input information class e = new employees(idnumber, firstname, lastname, annualsalary, startdate); //if data missing, user receive , error message if (idnumber.isempty()||firstname.isempty()||lastname.isempty()||annualsalary.isempty()||startdate.isempty()){ infooutput.settext("please fill out catagories!"); } //otherwise, information provided stored else { list.add(e); infooutput.settext(""); infooutput.settext("employee data added!"); } } private void removebuttonactionperformed(java.awt.event.actionevent evt) { //reset infooutput infooutput.settext(""); employees e; //declaring strings string idnumber, firstname, lastname, annualsalary, startdate; //reads input fields idnumber = idinput.gettext(); firstname = fnameinput.gettext(); lastname = lnameinput.gettext(); annualsalary = annualinput.gettext(); startdate = startdateinput.gettext(); //sends input information class e = new employees(idnumber, firstname, lastname, annualsalary, startdate); //determines if requested employee exists in list (int index = 0; index < list.size(); index++) { //if employee id found, information removed if (list.get(index).tostring() == idnumber){ infooutput.settext("employee data removed!"); list.remove(index); } //otherwise, error message displayed user else{ infooutput.settext("the employee id " + idnumber + " not found!"); } } } class employees { string idnumber, firstname, lastname, annualsalary, startdate; employees(string idnumber, string firstname, string lastname, string annualsalary, string startdate) { idnumber = idinput.gettext(); firstname = fnameinput.gettext(); lastname = lnameinput.gettext(); annualsalary = annualinput.gettext(); startdate = startdateinput.gettext(); } } private void listbuttonactionperformed(java.awt.event.actionevent evt) { //reset temp string temp = ""; //list of stored data (int x = 0; x <= list.size()-1; x++) { temp = temp + list.get(x).idnumber + " " + list.get(x).firstname + " " + list.get(x).lastname + " " + list.get(x).annualsalary + " " + list.get(x).startdate + "\n"; } outputfield.settext(temp); }
thanks help!
the constructor employees not initialize instance variables, initializes parameters. example, initialize idnumber as
this.idnumber = idnumber;
do similar statements remaining instance variables. why values in employee class null.
when testing idnumber, comparing tostring() of employee idnumber. must compare idnumber of employee in list idnumber ui.
if (list.get(index).idnumber.equals(idnumber)) {
it better create accessor each property in employee class.
Comments
Post a Comment