Hi, so I have already posted once, about another program. That didn't work so well, so I went back a step.
So I want a program, that will read a list of simple math problems from a text file. They would like in the file
4 + 3
2 * 3
12 / 4
ect.
So, I want the program to read the file, go line by line. Then split each line into, the 2 integers, and the operator. Than I use the if else stuff, to do the calculations.
So, I have something that should do that. But when I run it, all the answers are zero. Can someone have a look, see if I have made a mistake, or if I am doing it wrong. THanks
import java.io.*; class Calculator { public static void main(String args[]){ int z = 0; try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("C:/Users/Dylanka/Desktop/Test1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { String[] Parts = strLine.split(" "); String op = Parts[1]; int x = Integer.parseInt(Parts[0]); int y = Integer.parseInt(Parts[2]); { if(op == "+"){ z =x + y; } else if(op == "-"){ z = x - y; } else if(op == "/"){ z = x / y; } else if (op == "*"){ z = x * y; } System.out.println(x + " " + op + " " + y + " = " + z); } } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }