0

I'm having some issues with a java calculator assignment I was handed. I was told to make a calculator that does very basic functions, catches exceptions and allows you to correct values for either operand or operator immediately (which is what I'm having problems with). For instance, this is what should happen in the console:

j * 6
Catch exception and print error message here and asking for new first operand
4
Answer: 4 * 6 = 24

or

8 h 9
Catch exception and print error message here asking for new operator
+
Answer: 8 + 9 = 17

This code is what I have so far:

import java.util.*;
public class Calculator{

static int _state = 3;

public static void main(String[] args){

_state = 3;

System.out.println("Usage: operand1 operator operand2");
System.out.println(" (operands are integers)");
System.out.println(" (operators: + - * /");
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
do{

try{
int result = 0;
int operand1 = 0;
int operand2 = 0;

String operator = "";
char op = ' ';

operand1 = in.nextInt();

operator = in.next();
op = operator.charAt(0);
operand2 = in.nextInt();

switch (op){

    default:
        System.out.println("You didn't insert a proper operator");
        break;
    case '+': result = operand1 + operand2;
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '-': result = operand1 - operand2;
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '*': result = operand1 * operand2;   
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '/': result = operand1 / operand2;   
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;
        }

}
    catch(ArithmeticException e){
       System.out.println("You can not divide by zero. Input a valid divider.");
    }
    catch (InputMismatchException e) {
      System.out.println("You must use proper numerals.");
  }   
} while(_state == 3);

}
}
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85

3 Answers3

0

You need to do a few things before completing this. I would recommend creating your own Exceptions by extending the Exception class (or a subclass of Exception class). You can do this by reading the following:

How to create custom exceptions in Java?

Now say you created MissingOperatorException, then you can throw it in the Switch/Case statements.

try{
    switch (op){

        default:
            System.out.println("You didn't insert a proper operator");
            throw MissingOperatorException;
            break;
        //some more code here
    }
}
//reach catch statements
catch(MissingOperatorException e){
   System.out.println("ERROR MESSAGE");
   //deal with case here by asking for new input
   //use scanner to get operand
   //you can do this by reusing code from above
   System.out.println("Please re-enter operand");
   operator = in.next();
   op = operator.charAt(0);
   //calculate answer again
   //print out answer
}

You'll need to do this for every type of exception you are required to do. I see three possible cases.

  1. missing operator
  2. missing left operand
  3. missing right operand

There could be a combination of these problems, but the Exception handling you write should be able to take care of it as it goes (one by one).

NOTE: You have used the Scanner class to read in the user input. I would assume you understand the basics of Scanner, but your follow up questions contradict that. I would really research on the Java Scanner class. Please check this out:

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

Community
  • 1
  • 1
0

It is considered bad practice to have a giant try block that catches everything. Put the try catch block where you need it.

Also people don't really like doing other's homework for them. If you have a specific problem or question, that is fine, but saying, "i have to do this for an assignment how do I do all of it" probably isn't going to fly.

0

You should try this

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        boolean ret = true;
        while(ret){

        Scanner scn = new Scanner(System.in);
        String answer = "";
        String answer2 = "";
        System.out.println("Welcome to java calculator.....");
        System.out.print("\nEnter the first number: ");
        double numA = scn.nextDouble();
        System.out.print("Enter the second number: ");
        double numB = scn.nextDouble();
        System.out.println("What you want to calculate?");
        double result = 0;

        answer = scn.next();

        if(answer.equals("+")){
            result = numA + numB;
        }

        if(answer.equals("-")){
            result = numA - numB;
        }

        if(answer.equals("*")){
            result = numA * numB;
        }

        if(answer.equals("/")){
            result = numA / numB;
        }

        System.out.println("The result is: " + result);

        System.out.println("Do you want to continue?");
        answer2 = scn.next();

        if(answer2.equalsIgnoreCase("y")){
            ret = true;
            }

        else{
            System.out.println("Good bye....:)");
            System.exit(0);
            }
        }
    }
}