Results 1 to 3 of 3
- 10-27-2011, 08:53 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
Math program problem. Can I have a bit of help please :D
Hi, so, I am currently doing a Math type program. Basically, I want it to open up a text file, inside the file, it has simple math calculations, like:
4 + 2
3 * 2
5 / 5
So, I want it to solve these equations. I know, I have to read the file, line by line, loop it, and split the line into x, y and the operator. It will then do the calculations, by testing if it is certain operators. Then it will output the original problem, and the answer.
But, what I currently have, is not working. All the answers are coming out as 0, so I dunno what I have done wrong. Here is the code.
Any help will be appreciated. It is not displaying any errors, I have probably just made a really stupid mistake :pJava 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:/Test1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine = null; //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()); } } }
- 10-27-2011, 08:59 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
Re: Math program problem. Can I have a bit of help please :D
You should use equals instead of ==
op.equals("+"),.... and so on...
- 10-27-2011, 09:06 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Need help ASAP on a math program
By Swiper in forum New To JavaReplies: 11Last Post: 08-13-2011, 06:16 AM -
help w/ PI Math java program
By clemsontigers in forum New To JavaReplies: 12Last Post: 02-17-2011, 07:18 AM -
Help need on math java program
By zidangus in forum New To JavaReplies: 27Last Post: 07-07-2010, 02:48 AM -
Math Program
By stlboi in forum New To JavaReplies: 8Last Post: 04-02-2009, 09:34 PM -
need help with math for a new program
By gotenks05 in forum New To JavaReplies: 13Last Post: 09-26-2008, 07:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks