0

I am a little confused about how to implement System.exit(0); in my program. What I want to have happen is for the user to type the word "exit" and for the program to terminate. I would also like the program to return "0.0" when "clear" is entered, but I think I am having trouble working with my variables because num1 and num2 are doubles but I want "exit" and "clear" to be acceptable inputs, as well.

If anyone has any insight, that would be greatly appreciated. Here is my code:

package projectapcs;

/**
 *
 * @author Apple
 */

import java.util.Scanner;

public class ProjectAPCS {

    public static void exit(int x){

    }

    public static void clear(int y){

    }

    public static void main(String[] args) {



    double num1;
    double num2;
    char char1;
    String operation;


    Scanner input = new Scanner(System.in);

    System.out.println 
    ("Enter 'exit' to quit the calculator, 'help' for more options or 'continue' to use the   calculator");


    System.out.println
    ("\nEnter the first number:");
    num1 = input.nextInt();



     System.out.println 
     ("Display:" +  num1);

     System.out.println("Please enter operation:");
    operation = input.next();



    System.out.println("Enter the second number:");
    num2 = input.nextInt();
    System.out.println ("Display:" + num2);





    if ("+".equals(operation)){

        System.out.println((num1 + num2));
    } 
    else if ("-".equals(operation)){ 

        System.out.println((num1 - num2));
    }
    else if ("/".equals(operation)){ 

        System.out.println((num1 / num2));
    }
    else if ("*".equals(operation)){ 

        System.out.println((num1 * num2));
    }
    else if ("-x".equals(operation)){

        System.out.println (-num1);
    }
    else if ("x^2".equals(operation)){

        System.out.println (num1 * num1);
    }
    else if ("sqrt".equals(operation)){

        System.out.println (Math.sqrt(num1));

    }


    else if ("H".equals(num1)){

       System.out.println("Type + for the addition of values");
       System.out.println("Type - for the substraction of values");
       System.out.println("Type * for the multiplcation of values");
       System.out.println("Type / for the division of values");
       System.out.println("Type negate for the negation of values");
       System.out.println("Type ^2 for squaring of values");
    } 

    else if("E".equals(num1)){

       System.out.println ("Calculator program terminated...");
       System.exit(0);
    }

  }

}
1
  • 1
    In java you are exiting just by writing "return;" Commented Oct 11, 2013 at 1:48

1 Answer 1

1

If you want to allow arbitrary inputs, read input as a String, then convert to double as needed:

String input = scanner.next();
if (input.equals("exit")) {
    exit();
else if (input.equals("clear")) {
    clear();
} else {
    try {
        double number = Double.parseDouble(input);
        /* do something with `number` */
    } catch (NumberFormatException e) {
        System.err.println("Not a double.");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@MadProgrammer It would be more helpful if you said what's actually wrong with it. I don't use Java very often anymore..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.