Home > Mobile >  The output of this code are repeated. I can't get only one output
The output of this code are repeated. I can't get only one output

Time:12-21

for (int a = 0; a < noofvisitors; a  ) {
    String visitorname = Helper.readString("\nEnter visitor name > ");
    for (int j = 0; j < visitorList.size(); j  ){
        if (visitorname.equalsIgnoreCase(visitorList.get(j).getVname())) {
            System.out.println("\n*** Visitor already regisitered ***");
        } else {
            String visitornric4 = Helper.readString("Enter visitor last 4 alphanumeric digit of nric > ");
            int visitorcontact = Helper.readInt("Enter visitor contact > ");
            String date = Helper.readString("Enter visiting date > ");
            Visitor visitorinfo = new Visitor(visitorname, visitornric4, visitorcontact, date, patientname);
            visitorList.add(visitorinfo);
            visitorinfo.displayvisitorinfo();
            System.out.println("\n*** Visitor is regisitered successfully ***");
            patientList.get(i).setVisitorCount(noofvisitors);
            System.out.println("\n*** Please proceed to ward ***");

I am trying to have it output "\n*** Visitor already regisitered ***

CodePudding user response:

You need to compare the entered visitorname to all the names in visitorList before you can tell if it already exists. Therefore your loop is incorrect. You can change the logic to first iterate over all the elements, and only proceed to add a new Visitor if no match is found, but that won't be very efficient.

It will be much more efficient if you replaced your visitor List with a HashMap<String,Visitor>.

Then you can check if a visitor name already exists in expected constant time (instead of the linear time required by the loop):

    if (visitorMap.containsKey(visitorname.toLowerCase()) {
        System.out.println("\n*** Visitor already regisitered ***");
    } else {
        String visitornric4 = Helper.readString("Enter visitor last 4 alphanumeric digit of nric > ");
        int visitorcontact = Helper.readInt("Enter visitor contact > ");
        String date = Helper.readString("Enter visiting date > ");
        Visitor visitorinfo = new Visitor(visitorname, visitornric4, visitorcontact, date, patientname);
        visitorMap.put(visitorname.toLowerCase(),visitorinfo);
        visitorinfo.displayvisitorinfo();
        System.out.println("\n*** Visitor is regisitered successfully ***");
        patientList.get(i).setVisitorCount(noofvisitors);
        System.out.println("\n*** Please proceed to ward ***");
   }

CodePudding user response:

You need to iterate the entire list before you can decide if the user is already registered or not.

boolean alreadyRegistered = false;
for (int j = 0; j < visitorList.size(); j  ){
  if (visitorname.equalsIgnoreCase(visitorList.get(j).getVname())) {
    alreadyRegistered = true;
    break;
  }
}

Or, more easily:

boolean alreadyRegistered = visitorList.anyMatch(v -> visitorname.equalsIgnoreCase(v.getVname()));

Then you can decide what to do:

if (alreadyRegistered) {
  // Print message about being already registered.
} else {
  // Get the details of the new visitor.
}
  •  Tags:  
  • java
  • Related