-1

I'm trying to make a simple "store" program with a prompt to exit the program at the end. If the user types yes, it will exit. But if they say no, it's supposed to just print a message to let them know they can keep shopping. For some reason that message doesn't appear though. Here is the object.

public class HelloShopper1 {
   public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              System.out.print("Hello Shopper, what is your name?\n");
              String input = scanner.nextLine();
              if(!input.matches("[a-zA-Z_]+")) {
              System.out.println("Nice try " + input + " . Get out of my store!");
              } else {
             System.out.println("Thank you, " + input + ". It is my pleasure to help you today.");
             System.out.println("Do you want to close this program?");
             String input1 = scanner.nextLine();
             System.out.println(input1);
             if(input1 == "yes") {
             System.exit(0);
             if(input1 == "no") {
             System.out.println("Thank god. Please continue shopping.");
           }
        }
    }
}
minus xp
  • 1
  • 1
  • 1
    Don't use `==` to compare Strings. Use the `.equals(...)` or `.equalsIgnoreCase(...)` method. Understand that `==` checks for *reference* equality while the methods instead do what you're really interested in testing: for *functional* equality. – DontKnowMuchBut Getting Better Sep 16 '17 at 19:03

1 Answers1

0
import java.util.*;
public class HelloShopper1 
{
public static void main(String[] args) 
{
          Scanner scanner = new Scanner(System.in);
          System.out.print("Hello Shopper, what is your name?\n");
          String input = scanner.nextLine();
          if(!input.matches("[a-zA-Z_]+")) 
          {
          System.out.println("Nice try " + input + " . Get out of my 
       store!");
          }else{
         System.out.println("Thank you, " + input + ". It is my pleasure to 
         help you today.");
         System.out.println("Do you want to close this program?");
         String input1 = scanner.nextLine();
         if(input1.equalsIgnoreCase("yes")) 
         {
         System.exit(0);
         }
         if(input1.equalsIgnoreCase("no")) 
         {
         System.out.println("Thank god. Please continue shopping.");
         }
        }
       }
       }
JaNaM SoNi
  • 77
  • 4