Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Math program problem.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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());
    		  }
    	  }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    Recommended reading: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Math program problem.

    Quote Originally Posted by mwr76 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. help w/ PI Math java program
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 16th, 2011, 09:43 PM
  2. My program doesnt want to do its math point my errors out please
    By Redlight in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 01:56 PM
  3. Help need on math java program
    By zidangus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 6th, 2010, 07:41 PM
  4. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  5. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM