import java.io.*;
public class Calculator{

public static void main(String[] args){
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
int x=1, y=1;
String Str_1,Str_2;
System.out.println("Enter an Equation: ");
try
{
Str_1=dataIn.readLine();
System.out.print("Enter another Equation: ");
Str_2=dataIn.readLine();
x=Integer.parseInt(Str_1);
y=Integer.parseInt(Str_2);
x = x+y;
x = x-y;
x = x*y;
x = x/y;
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero ERROR.");
}
catch(NumberFormatException e)
{
System.out.println("Invalid number entered.");
}
catch(Exception e)
{
System.out.println("Invalid number entered.");
}
finally
{
int add=x+y;
int minus=x-y;
int multiply=x*y;
int divide=x/y;

switch (x)
{
case'+':
System.out.println("The answer is: "+add);
break;

case'-':
System.out.println("The answer is: "+minus);
break;

case'*':
System.out.println("The answer is: "+multiply);
break;

case'/':
System.out.println("The answer is: "+divide);
break;
}
}
}
}

when i enter an equation it doesn't give me answers... i need to know what's the problem and how it will work. thanks.

One, do some formatting and indentation. It's impossible to read as it is.

int x=1, y=1;
String Str_1,Str_2;
System.out.println("Enter an Equation: ");
try
{
Str_1=dataIn.readLine();
System.out.print("Enter another Equation: ");
Str_2=dataIn.readLine();
x=Integer.parseInt(Str_1);
y=Integer.parseInt(Str_2);
x = x+y;
x = x-y;
x = x*y;
x = x/y;
}

Lines 3 and 7 - I have no idea what I'm supposed to enter here, so I'll enter:

5x + 7y = 87

and

10*k - (87 * t^4) = 65

Is that what you want? You asked for two equations and I gave you two equations. If that isn't what you want me to type in, you need to tell me what I need to type in. "Type in an equation" is too vague.

Lines 9 and 10 - You asked for two equations, but you are parsing them into two integers. Do you want two equations or do you want two integers?

Lines 11 - 14 - Why do you keep overwriting x here?

switch (x)
{
case'+':
System.out.println("The answer is: "+add);
break;

case'-':
System.out.println("The answer is: "+minus);
break;

case'*':
System.out.println("The answer is: "+multiply);
break;

case'/':
System.out.println("The answer is: "+divide);
break;
}

Lines 1, 3, 7, 11, and 15. x is an integer, right? Or is it a '+', '-', '*', or '/'?

I have an x and a y, presumably intended to be numbers, and I should probably have a character somewhere that stores a '+', '-', '*', or '/'. But I don't see any such variable in your code.

You need to decide exactly what input you want from the user, you need to tell the user what to type in, and then you need to parse their input and store it in a way that you can use it in your calculator.

This article has been dead for over six months. Start a new discussion instead.