[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

if { }

System.out.println("Enter the record number");
int record = SavitchIn.readLineInt();
Patient p = hs.searchPatient(record);
                                     
if(p==null)
{
       System.out.println("The patient is not valid");
}
                                     
if(hs.removePatient(p))
{
        System.out.println(p.getName() + " deleted. ");
}
else
{
       System.out.println(" Error " + p.getName() + " not deleted. ");
}




The code above uses vectors. This part of the code

Patient p = hs.searchPatient(record);

search for the patient and returns and object of the patient. If the patient cannot be found, it will return a null value.

so if

if(p==null)
{
       System.out.println("The patient is not valid");
}

the msg is displayed. However in my case this part of the code:

if(hs.removePatient(p))
{
        System.out.println(p.getName() + " deleted. ");
}
else
{
       System.out.println(" Error " + p.getName() + " not deleted. ");
}

gets executed as well.



How do I solve this problem?.

Your help is kindly appreciated.

Regards

Eugene


0
eugene007
Asked:
eugene007
  • 2
1 Solution
 
bloodredsunCommented:
if(p==null)
{
       System.out.println("The patient is not valid");
}else{
                                 
if(hs.removePatient(p))
{
        System.out.println(p.getName() + " deleted. ");
}
else
{
       System.out.println(" Error " + p.getName() + " not deleted. ");
}
}
0
 
eugene007Author Commented:
why does it this happen.

System.out.println("The patient is not valid");

does not stop the flow.
0
 
CEHJCommented:
The flow should really be to get the user to repeat the process until a valid number has been entered, so you could do something like:


while(hs.removePatient(hs.searchPatient(SavitchIn.readLineInt())) == false) {
      System.out.println("Error: incorrect patient number or patient not deleted.");
      System.out.print("Enter the record number: ");
}

0
 
bloodredsunCommented:
Sorry, why does what happen? Are you asking why the program doesn't stop after executing that line?

System.out.println() is just a method that returns nothing so once it has been executed, the program carries on.
Imagine this scenario:

System.out.println("Hello");
System.out.println("there");

If the first line stopped, then you would never be able to print more than a single line.

I suggest that you look a bit more closely into the control structures of Java such as for, while if/else. Here's a link http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now