0

I have an assignment to create an ArrayList of employees, to provide a menu to add, find, and delete employee records. I successfully managed to implement all the functions on my own, but there is a small problem. When I use the find or delete option, the correct record is found or deleted properly but the code goes through the array list of elements and prints out employee not found till the correct record is found, this is unnecessary as it should only print the found record. I have limited experience with coding and I am writing my own code from scratch, please help me with this. enter image description here

I'VE ATTACHED MY CODE AND THE OUTPUT!

else if (choice.equals("4")) {
    System.out.println("Enter Name: ");
    String fName = myScanner.nextLine();
    System.out.println("Enter Job Name: ");
    String fJob = myScanner.nextLine();
    for (int i = 0; i < myEList.size(); i++) {
        if (myEList.get(i).getName().equals(fName) && myEList.get(i).getJob().equals(fJob)) {
            System.out.println("Employee found!");
            System.out.println(myEList.get(i).toString());
        } else {
            System.out.print("Employee not found!");
        }
    }
} else if (choice.equals("5")) {
    System.out.println("Enter Name: ");
    String dName = myScanner.nextLine();
    System.out.println("Enter Job Name: ");
    String dJob = myScanner.nextLine();
    for (int i = 0; i < myEList.size(); i++) {
        if (myEList.get(i).getName().equals(dName) && myEList.get(i).getJob().equals(dJob)) {
            System.out.println("Employee record removed succesfully!");
            myEList.remove(i);
        } else {
            System.out.print("Employee not found!");
        }
    }
}

This is the Output Enter Option: 4 Enter Name: arjun Enter Job Name: tester Searching... Employee not found! Employee not found! Employee found! Name: arjun Job Name: tester Weekly Pay: 1200.0

Jérôme
  • 1,254
  • 2
  • 20
  • 25
Vikram AJ
  • 1
  • 1
  • Just remove youe else condition having Sysout System.out.print("Employee not found!"); – mhasan Mar 11 '17 at 15:14
  • You can only know that you haven't found an employee **after** the loop, when you have checked all employees of the list. So the "not found" message can't possibly be inside the loop. The only coorect message that you could have inside the loop is "this employee is not the right one". Use a `found` boolean variable. When you find the employee, set it to true and exit the loop. Then, out of the loop, print nof found if the variable is still false. – JB Nizet Mar 11 '17 at 15:14

3 Answers3

0
else 
                    {
                        System.out.print("Employee not found!");
                    }

This has to be AFTER the for loops are over. You need a found flag. If after the loop is false then print the message

bichito
  • 1,406
  • 2
  • 19
  • 23
0
  1. Create a variable to track if employee not found before the loops

  2. In the if statements sent it to false if found.

  3. At the end of the loop check if it is true. Then print the message "Employee not found"

See the sample below

boolean notFound = true;
for(int i=0;i<myEList.size();i++)
{
     if(myEList.get(i).getName().equals(fName)&&myEList.get(i).getJob().equals(fJob))
     {
        System.out.println("Employee found!");
        System.out.println(myEList.get(i).toString());
        notFound = false;
        break;
     }
}
if(notFound)
    System.out.print("Employee not found!");
hmatar
  • 2,437
  • 2
  • 17
  • 27
-1

Remove this block of code

else 
                {
                    System.out.print("Employee not found!");
                }

It should work fine after that.


EDIT

This is off topic but when you ask question next time remember to post image of output rather than typing it or posting a link.

And also avoid writing about your coding experience. Just be straight to the point. It just increases the size of question unnecessarily. :)