Creating a calculator


#1

Hey guys,

i just finished the calculator project in the java course.

Now I’m wondering how i can give my calculator input-values via the console (only for adding and subtracting)?

As i googled along i found a so called “BufferedReader”, which could solve my problem.s

Can anyone help me, how and where to implement this BufferedReader in my code please?

Here is my code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//Firstly create a class
public class Calculator {

  //Secondly create a constructor or. initial state of an object - the calculator
  public Calculator () 
  {
	  
  }
  
  	//Thirdly define methods or. the operations of the calculator
  	public int add (int a, int b) 
  	{
 	return a+b;
 	}
  
  	public int subtract (int a, int b)
  	{
  	return a-b;
  	}

// Fourthly create a new Object "myCalculator"
  public static void main (String [] args) throws IOException 
  {
  Calculator myCalculator = new Calculator();
  
  //Fifthly print out the results of the calculator
  System.out.println(myCalculator.add(5,7));
  System.out.println(myCalculator.subtract(5,7));
  
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  String input = reader.readLine();
  }
  
}

I’m looking forward for your help!

Kind Regards
Philipp


#2
import java.util.Scanner;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int a = input.nextInt();
        System.out.println("Enter a number: ");
        int b = input.nextInt();
        System.out.println(myCalculator.add(a,b));

I recommend using Scanner as demonstrated.


#3

Hey,

thanks! With your solution i was able to implement the BufferedReader.

The code works just fine now - however when i type “+” or “-” in the console, the program always jumps in the else condition in “public void calculate (int a, int b, String operator)”

Do you have any idea why the program does not check the if and else if condition?

Here is my final code (i have adjusted it a little bit - i know, there is probably a much easier way to calculate two numbers by +/-. However the right answer to my problem would be awesome :slight_smile: ):

import java.io.*;

class Calculator {

static void calculate(int a, int b, String operator) {

	if (operator == "+") {
		int summe = a + b;
		System.out.println("Ihr Ergebnis beträgt " + summe);
	}

	else if (operator == "-") {
		int differenz = a - b;
		System.out.println("Ihr Ergebnis betrögt " + differenz);
	}

	else {
		System.out.println("Bitte verwende ein + oder ein - als Rechenoperator!");
	}
}

public static void main(String[] args) {

	// BufferedReader Objekt erstellen
	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

	// Zahlen einlesen und berechnen
	try {
		// Einlesen der Variable a
		System.out.println("Bitte gebe hier die erste Zahl ein:");
		int a = Integer.parseInt(reader.readLine());

		// Einlesen der Variable b
		System.out.println("Bitte gebe hier die zweite Zahl ein:");
		int b = Integer.parseInt(reader.readLine());

		// Einlesen des Rechenoperators
		System.out.println("Bitte gebe hier den Rechenoperator (+ oder -) ein:");
		String operator = reader.readLine();
		calculate(a, b, operator);
	}

	catch (IOException e) {
		System.out.println("Fehler beim Einlesen der Zahl!");
	}
}

}

Edit: If you want, i translate the comments and syso prints for you (sorry, they are written in german).


#4

That was a bit of a headache to debug!

It’s kind of a silly solution but instead of
String operator = reader.readLine();
do
char operator = reader.next().charAt(0);

and have the calculate method take a char instead of string

I’m not sure why it didn’t like the string yet! But that quick hack will work
edit: I used scanner not bufferedReader
I’m sure they work similarly though.
Quick fix code with exception handling removed

import java.util.Scanner;

class Main {
  static void calculate(int a, int b, char operator) {
  
	if(operator == '+') {
		int summe = a + b;
		System.out.println("Ihr Ergebnis beträgt " + summe);
	}

	else if (operator == '-') {
		int differenz = a - b;
		System.out.println("Ihr Ergebnis betrögt " + differenz);
	}

	else 
		System.out.println("Bitte verwende ein + oder ein - als Rechenoperator!");
}

public static void main(String[] args) {

	// BufferedReader Objekt erstellen
	Scanner input = new Scanner(System.in);

	// Zahlen einlesen und berechnen
		// Einlesen der Variable a
	System.out.println("Bitte gebe hier die erste Zahl ein:");
	int a = input.nextInt();
	// Einlesen der Variable b
	System.out.println("Bitte gebe hier die zweite Zahl ein:");		
  int b = input.nextInt();
	// Einlesen des Rechenoperators
	System.out.println("Bitte gebe hier den Rechenoperator (+ oder -) ein:");		
  char operator = input.next().charAt(0);
	calculate(a, b, operator);
  }
}