I make a calculator in which we enter an expression and it shows us the result.
import java.io.File;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter expression: ");
String expression = scan.nextLine();
try {
File file = new File("expression.txt");
if(!file.exists()) {
file.createNewFile();
}
PrintWriter pw = new PrintWriter(file);
pw.print(expression);
pw.close();
String pythonFile = "calculate.py";
Process process = Runtime.getRuntime().exec("chmod -R 777 python " + pythonFile);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
}
I work on the phone and use Replit for work. The problem is that it shows us not the result, but null.
Also my python code.
expressionFile = open('expression.txt', 'a+'))
expression = str(expressionFile.read())
result = eval(expression)
expressionFile.wtite("=" + str(result))
print(expressionFile.read())
expressionFile.close()
I tried using ProcessBuilder and change the path calculate.py, but it didn’t help.
New contributor
Ilarion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.