title: How do I fix my Java code so that it ends when a user types "quit"?
body: <p>So I have been working on this simple calculator in Java for a while now, and I want the program to end when a user types "quit" in the scanner. I tried to implement this feature as an 'If' statement near the end of my main class in the following line:</p>
  
  <pre><code>if (input.equals("quit")) {
                  System.out.println("Thanks for using my program.");
                  System.exit(0);
              } 
  </code></pre>
  
  <p>My IDE doesn't recognize any errors, but when I compile and run my program, and attempt to exit the program by typing 'quit' into the scanner, the program ends (with errors) without displaying my exit message. </p>
  
  <p>My code is as follows: </p>
  
  <pre><code> package calculator;
      import java.util.Scanner;
  
      public class Calculator {
  
      public static void main(String[] args) {
  
          System.out.println("Hello, welcome to my calculator");
          System.out.println("Enter in some stuff you want to me to calculate");
  
          Scanner scan = new Scanner(System.in);
          System.out.println("If you need help please type \"help\"");
          System.out.println("If at anytime you want to leave, type \"quit\"");
          System.out.println("If you want to continue, hit enter.");
  
          String s1 = scan.nextLine();
  
          if (s1.equals("help")){
              System.out.println(" ");
              System.out.println("Double operand commands:");
              System.out.println("Addition: '+' (Ex: 'a + b' )");
              System.out.println("Subtraction: '-' (Ex: 'a - b' )");
              System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
              System.out.println("Division: '/' (Ex: 'a / b' )");
              System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
              System.out.println(" ");
          }
  
          Scanner input = new Scanner(System.in);
  
          Maths maths = new Maths();
  
          double answer = 0;
          double numA, numB;
          char operator;
          boolean quit = false;
  
           while (quit == false) {
              System.out.print("Please enter your equation: ");
  
              numA = input.nextDouble();
              operator = input.next().charAt(0);
              numB = input.nextDouble();        
  
              if (operator == '+') {
                  answer = maths.add(numA, numB);
              }
  
              if (operator == '-') {
                  answer = maths.subtract(numA, numB);
              }
  
              if (operator == '*') {
                  answer = maths.multiply(numA, numB);
              }
  
              if (operator == '/') {
                  answer = maths.divide(numA, numB);
              }
  
              if (operator == '^') {
                  answer = maths.power(numA, numB);
              }
  
              System.out.println(answer);        
  
          if (input.equals("quit")) {
              System.out.println("Thanks for using my program.");
              System.exit(0);
          }
  
           }       
  
          input.close();
  
          }
  
      }
  
      class Maths {
  
          double add(double a, double b) {
              double answer = a+b;
              return answer;          
          }
  
          double subtract(double a, double b) {
              double answer = a-b;
              return answer;          
          }
  
          double multiply(double a, double b) {
              double answer = a*b;
              return answer;          
          }
  
          double divide(double a, double b) {
              double answer = a/b;
              return answer;          
          }
  
          double power(double a, double b){
              double answer =a;
  
              for (int x=2; x&lt;=b; x++){
                  answer *= a;
              }
  
              return answer;
          }
  
      }
  </code></pre>
  
  <p>I would appreciate it if you could tell me what's wrong so that my code will finally work and so that I won't make a similar mistake in the future!</p>
  
  <p>Thanks in advance</p>