Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By SRaith

Thread: Math program problem. Can I have a bit of help please :D

  1. #1
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default 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.

    Java 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());
    		  }
    	  }
    }
    Any help will be appreciated. It is not displaying any errors, I have probably just made a really stupid mistake :p

  2. #2
    SRaith is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Math program problem. Can I have a bit of help please :D

    You should use equals instead of ==

    op.equals("+"),.... and so on...
    Fubarable likes this.

  3. #3
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Math program problem. Can I have a bit of help please :D

    Quote Originally Posted by SRaith View Post
    You should use equals instead of ==

    op.equals("+"),.... and so on...
    OMG, yay. Thanks soo much. Works. Wow. Spent ages, looking at everything, except that one part. Woops.

Similar Threads

  1. Need help ASAP on a math program
    By Swiper in forum New To Java
    Replies: 11
    Last Post: 08-13-2011, 07:16 AM
  2. help w/ PI Math java program
    By clemsontigers in forum New To Java
    Replies: 12
    Last Post: 02-17-2011, 08:18 AM
  3. Help need on math java program
    By zidangus in forum New To Java
    Replies: 27
    Last Post: 07-07-2010, 03:48 AM
  4. Math Program
    By stlboi in forum New To Java
    Replies: 8
    Last Post: 04-02-2009, 10:34 PM
  5. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 08:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •