1
import java.util.Scanner;
   public class Ozo {
    public static void main(String [] args) { 
     Scanner Henry = new Scanner (System.in);
     System.out.println("Hello Welcome to Algorithm Chemist\nHow may we 
     help you?");
     String name, stress;
     int age;
     System.out.println("Please what is your name?");
     name = Henry.nextLine();
     System.out.println("What is your stress?");
     stress = Henry.nextLine();
     System.out.println("How old are you?");
     age = Henry.nextInt();
     if (age <= 12 && stress == "HeadAce") {
      System.out.println("Take one tablet of Panadol Extra and one 
       tablet of Paracetamol");
     if (age >=13 && stress == "HeadAce") {
      System.out.println("Take two Tablet of Paracetamol and one tablet 
      of Panadol");
     if (age <= 12 && stress == "Malaria") {
      System.out.println("Take one dose of Alabukun or better advise go 
      for herbal medicine");
    if (age >= 13 && stress == "Malaria") {
     System.out.println("Take two dose of Alabukun and one tablet of 
     Postinol");
    System.out.println("Thank you for using our service, we sincerely appreciate your patronage");
    }
   }
  }
 }
}
}

I am creating a project in Java. And anytime I enter a value of >= 13 it does not display anything.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pardeep
  • 2,153
  • 1
  • 17
  • 37
  • Try to change: `stress = Henry.nextLine();` to: `stress = Henry.nextLine().strip();` – Nir Alfasi Sep 17 '17 at 04:04
  • You may refer to this link: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Eric Tung Sep 17 '17 at 04:19
  • 1
    the tests should be changed to `stress.equals("Malaria")`. feel free to refer to https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java for the details – Gilles Gouaillardet Sep 17 '17 at 04:21
  • Spelling: I think you mean "Headache", not "HeadAce". – halfer Sep 17 '17 at 09:38
  • If you put this code in an automatic reformatter, you'll see what the problem is - all of those conditions are nested. So, if the first one does not trigger, none of them will. – halfer Sep 17 '17 at 09:40

2 Answers2

3

Your brackets are incorrect. You'll want to refactor this to use if/else-if structure.

if (age <= 12 && stress == "HeadAce") {
    System.out.println("Take one tablet of Panadol Extra and one tablet of Paracetamol");
}else if (age >=13 && stress == "HeadAce") {
    System.out.println("Take two Tablet of Paracetamol and one tablet of Panadol");
}else if (age <= 12 && stress == "Malaria") {
    System.out.println("Take one dose of Alabukun or better advise go for herbal medicine");
}else if (age >= 13 && stress == "Malaria") {
    System.out.println("Take two dose of Alabukun and one tablet of Postinol");
}

The problem you have right now is all of your if's are nested. For instance the first two:

if (age <= 12 && stress == "HeadAce") {
    System.out.println("Take one tablet of Panadol Extra and one tablet of Paracetamol");
    if (age >=13 && stress == "HeadAce") {
        System.out.println("Take two Tablet of Paracetamol and one tablet of Panadol");
    }
}

The only way for the inner println will execute is if age <= 12 && stress == "HeadAce" && age >= 13 && stress == "HeadAce" and since age cannot be both less than 13 and greater than 12 it will never run that inner println.

Barak Gall
  • 1,422
  • 12
  • 24
1

That's because the second condition is nested with your formatting :

if (age <= 12 && stress.equals("HeadAce")) { // use equals for string value comparison
    System.out.println("Take one tablet of Panadol Extra and one tablet of Paracetamol");
} // you might want to place these
if (age >=13 && stress.equals("HeadAce")) {
    System.out.println("Take two Tablet of Paracetamol and one tablet of Panadol");
}
if (age <= 12 && stress.equals("Malaria")) {
    System.out.println("Take one dose of Alabukun or better advise go for herbal medicine");
}
if (age >= 13 && stress.equals("Malaria")) {
     System.out.println("Take two dose of Alabukun and one tablet of Postinol");
}
System.out.println("Thank you for using our service, we sincerely appreciate your patronage");

And because of that nesting, the inner if is not reached. From your implementation, seems like you should be closing the if and then using subsequent calls should be made as shared above.

Naman
  • 27,789
  • 26
  • 218
  • 353