# Math program problem.

• October 27th, 2011, 12:10 PM
dylanka
Math program problem.
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

Code :

```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()); } } }```
• October 27th, 2011, 12:20 PM
KevinWorkman
Re: Math program problem.
At what point in the program does what is happening diverge from what you want to happen? Are you sure you're reading the values in correctly? Are you sure you're splitting the values correctly? Are you sure you're converting them to numbers and calculations correctly? Are you sure you're doing the calculations correctly? Are you sure you're printing the values correctly?

Break this problem up into smaller pieces that each do a single thing, and figure out which step isn't working. Then we'll go from there.

• October 27th, 2011, 03:41 PM
mwr76
Re: Math program problem.
I tested your program and got it to work. You have to look at your op variable. You need to store a char there instead of a single character string. Look at the charAt(0) method. When you evaluate in your if statements use chars ' ' in stead of string literals " ".

Hope it helps
• October 27th, 2011, 03:44 PM
KevinWorkman
Re: Math program problem.
Quote:

Originally Posted by mwr76
I tested your program and got it to work. You have to look at your op variable. You need to store a char there instead of a single character string. Look at the charAt(0) method. When you evaluate in your if statements use chars ' ' in stead of string literals " ".

Hope it helps

For the record, I think that's only part of the problem. Hint: == != .equals().

mwr76, this is why we usually try to let the OP figure it out for themselves. It's much more educational.