Hey guys! I'm working on a console calculator. My problem is that how would I perform the inputted operator? So, if the user inputs "+" and a "-" it will perform those. Here's what I have so far:

import java.util.Scanner;

public class Calculator
{
	public static void main (String []args)
	{
		Scanner in = new Scanner (System.in);
		int firstInput;
		int secondInput;
		int thirdInput;
		String firstOperator;
		String secondOperator; 
		int result;
			
		System.out.print ("1st input: ");
		firstInput = in.nextInt();
		System.out.print ("1st Operator: ");
		firstOperator = in.next();
		System.out.print ("2nd input: ");
		secondInput = in.nextInt();
		System.out.print ("2nd Operator: ");
		secondOperator = in.next();
		System.out.print ("3rd input: ");
		thirdInput = in.nextInt();
		
		
		
		if (firstOperator == "+" && secondOperator == "+" )
		{
			result = firstInput + secondInput + thirdInput; 
			System.out.println ("The Expression: "+firstInput+firstOperator+secondInput+secondOperator+thirdInput);
			System.out.println ("The Result is: " +result);
		}
		
	}
	
}

Recommended Answers

All 7 Replies

if (firstOperator == "+" && secondOperator == "+" )

To compare a string you should use equals method.
So it should be

if (firstOperator.equals("+") && secondOperator.equals("+")}

Oh, thanks man. But also, my problem is how am I going to assign the addition operator to the firstOperator or secondOperator? So that when the user inputs any operators (+, - , *, /, %) it will perform it?

In this case , you should check what operator is entered by user using IF condition.
So place a IF condition for every operator.

Otherwise use switch-case statement to make the task easy..

My teacher wants us to use IF conditions. So would it look like something like this:

if (firstOperator.equals("+"))
result = firstInput + secondInput;

My teacher wants us to use IF conditions. So would it look like something like this:

yes.
if-else-if-

if (firstOperator.equals("+"))
result = firstInput + secondInput;
else if (firstOperator.equals("-"))
result = firstInput - secondInput;
else if (firstOperator.equals("/"))
result = firstInput / secondInput;
............

Thanks for your advice! But now my problem is printing the "result". An error pops up and says variable "result" might not have been initialized.
Here's what I have now:

import java.util.Scanner;

public class Calculator
{
	public static void main (String []args)
	{
		Scanner in = new Scanner (System.in);
		int firstInput;
		int secondInput;
		int thirdInput;
		String firstOperator;
		String secondOperator; 
		int result;
			
		System.out.print ("1st input: ");
		firstInput = in.nextInt();
		System.out.print ("1st Operator: ");
		firstOperator = in.next();
		System.out.print ("2nd input: ");
		secondInput = in.nextInt();
		System.out.print ("2nd Operator: ");
		secondOperator = in.next();
		System.out.print ("3rd input: ");
		thirdInput = in.nextInt();
		
		
			if (firstOperator.equals("+") && secondOperator.equals("+"))
				result = firstInput + secondInput + thirdInput; 
			
			else if (firstOperator.equals("+") && secondOperator.equals("-"))
				result = firstInput + secondInput - thirdInput; 
			
			else if (firstOperator.equals("+") && secondOperator.equals("*"))
				result = firstInput + secondInput * thirdInput; 		
					
			else if (firstOperator.equals("+") && secondOperator.equals("/"))
				result = firstInput + secondInput / thirdInput; 
			
			else if (firstOperator.equals("+") && secondOperator.equals("%"))
				result = firstInput + secondInput % thirdInput; 
								
			else if (firstOperator.equals("-") && secondOperator.equals("+"))
				result = firstInput - secondInput + thirdInput; 
			
			else if (firstOperator.equals("-") && secondOperator.equals("-"))
				result = firstInput - secondInput - thirdInput; 	
							
			else if (firstOperator.equals("-") && secondOperator.equals("*"))
				result = firstInput - secondInput * thirdInput; 
			
			else if (firstOperator.equals("-")&& secondOperator.equals("/"))
				result = firstInput - secondInput / thirdInput; 	
					
			else if (firstOperator.equals ("-") && secondOperator.equals("%"))
				result = firstInput - secondInput % thirdInput; 	
		
			else if (firstOperator.equals("*") && secondOperator.equals("+"))
				result = firstInput * secondInput + thirdInput; 
			
			else if (firstOperator.equals("*") && secondOperator.equals("-"))
				result = firstInput * secondInput - thirdInput; 	
							
			else if (firstOperator.equals("*") && secondOperator.equals("*"))
				result = firstInput * secondInput * thirdInput; 
			
			else if (firstOperator.equals("*")&& secondOperator.equals("/"))
				result = firstInput * secondInput / thirdInput; 	
					
			else if (firstOperator.equals ("*") && secondOperator.equals("%"))
				result = firstInput * secondInput % thirdInput; 
			
			else if (firstOperator.equals("/") && secondOperator.equals("+"))
				result = firstInput / secondInput + thirdInput; 
			
			else if (firstOperator.equals("/") && secondOperator.equals("-"))
				result = firstInput / secondInput - thirdInput; 	
							
			else if (firstOperator.equals("/") && secondOperator.equals("*"))
				result = firstInput / secondInput * thirdInput; 
			
			else if (firstOperator.equals("/")&& secondOperator.equals("/"))
				result = firstInput / secondInput / thirdInput; 	
					
			else if (firstOperator.equals ("/") && secondOperator.equals("%"))
				result = firstInput / secondInput % thirdInput; 
		
			else if (firstOperator.equals("%") && secondOperator.equals("+"))
				result = firstInput / secondInput + thirdInput; 
			
			else if (firstOperator.equals("%") && secondOperator.equals("-"))
				result = firstInput / secondInput - thirdInput; 	
							
			else if (firstOperator.equals("%") && secondOperator.equals("*"))
				result = firstInput / secondInput * thirdInput; 
			
			else if (firstOperator.equals("%")&& secondOperator.equals("/"))
				result = firstInput / secondInput / thirdInput; 	
					
			else if (firstOperator.equals ("%") && secondOperator.equals("%"))
				result = firstInput / secondInput % thirdInput; 
					
			else if (thirdInput == 0)
				System.out.print("Cannot divide by zero");
				
				
		System.out.println("The expression is: "+firstInput+firstOperator+secondInput+secondOperator+thirdInput);
		System.out.print(result);

	}
	
}

An error pops up and says variable "result" might not have been initialized.

Initialize result with 0.on line thirteen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.