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 11 of 11

Thread: Help with five function calculator

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with five function calculator

    I have to create a five-function calculator. The input to my program will be a binary arithmetic expression, with the operator separated from the operands by a space like 24 * 4. The output will be the binary arithmetic expression, followed by an equal sign, surrounded by spaces, followed by the answer.

    I can assume that the operands will be floating-point numbers (use doubles). The operator should be one of the following: *, /, +, -, or %. Here are important additional specifications:

    -Emit an appropriate prompt (i.e. Please enter an equation with spaces between operands and operator.)

    -If the operator is not one of *, /, +, -, or %, issue an appropriate error message (i.e. Sorry, '&' is not recognized).

    -If operator is "/" and the second operand is zero, issue an appropriate error message.(i.e. Cannot divide by zero).

    Please help! I tried over and over again and my program will either not run or it won't compile. I don't know what I'm doing wrong.


     
    import java.util.Scanner;
     
    public class Calculator {
     
    	public static void main(String[] args) {
     
    		Scanner in = new Scanner(System.in);
     
    		System.out.printf("Enter a binary arithmetic expression, with spaces between operands and operator. \n");
    		double inputOne = in.nextDouble();
    		double inputTwo = in.nextDouble();
     
    		char operator = 0;
    		if (operator == '+')
    			System.out.printf("%f + %f = %f", inputOne, inputTwo, inputOne
    					+ inputTwo);
    		if (operator == '-')
    			System.out.printf("%f - %f = %f", inputOne, inputTwo, inputOne
    					- inputTwo);
    		if (operator == '*')
    			System.out.printf("%f * %f = %f", inputOne, inputTwo, inputOne
    					* inputTwo);
    		if (operator == '/')
     
    		{
    			if (inputTwo == 0) {
    				System.out.printf("Cannot divide by 0");
    			} else
    				System.out.printf("%f / %f = %f", inputOne, inputTwo, inputOne
    						/ inputTwo);
     
    		}
    		if (operator == '%') {
    			System.out.printf("%f % %f = %f", inputOne, inputTwo, inputOne
    					% inputTwo);
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with five function calculator

    Where does the code read the operator that the user is supposed to type in?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with five function calculator

    Ok, I changed it to where I thought the code would read the operator that the user is supposed to type in, but it is still crashing?



     
    import java.util.Scanner;
     
    public class CheapCalculator {
     
    	public static void main(String[] args) {
     
    		Scanner in = new Scanner(System.in);
     
    		System.out.printf("Enter a binary arithmetic expression, with spaces between operands and operator. \n");
    		double inputOne = in.nextDouble();
    		double inputTwo = in.nextDouble();
     
    		String operator = in.nextLine();
     
     
     
     
     
    		if (operator.equals ('+'))
    			System.out.printf("%f + %f = %f", inputOne, inputTwo, inputOne
    					+ inputTwo);
    		if (operator.equals ('-'))
    			System.out.printf("%f - %f = %f", inputOne, inputTwo, inputOne
    					- inputTwo);
    		if (operator.equals ('*'))
    			System.out.printf("%f * %f = %f", inputOne, inputTwo, inputOne
    					* inputTwo);
    		if (operator.equals ('/'))
     
    		{
    			if (inputTwo == 0) {
    				System.out.printf("Cannot divide by 0");
    			} else
    				System.out.printf("%f / %f = %f", inputOne, inputTwo, inputOne
    						/ inputTwo);
     
    		}
    		if (operator.equals('%')) {
    			System.out.printf("%f % %f = %f", inputOne, inputTwo, inputOne
    					% inputTwo);
    		}
     
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with five function calculator

    it is still crashing
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with five function calculator

    Enter a binary arithmetic expression, with spaces between operands and operator.
    3 + 4
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at CheapCalculator.main(CheapCalculator.java:12)

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with five function calculator

    Matching up the expected input to the correct Scanner method is often more trouble than it's worth, especially if the user is uncooperative. A more bullet-proof approach is to take the whole line of input as a String and parse that into the necessary parts using String.split() or perhaps some self-designed parser with appropriate input verification/error recovery methods.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with five function calculator

    I'm sorry, we haven't gotten to parsing or String.split(). That is over my head.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with five function calculator

    Another solution would be to use a try{}catch block around the Scanner methods that read in data from the user. Put that inside a while() loop that would keep looping if the user enters bad data.

    The posted code does not read the user's input in the same order as it was entered. It reads 2 numbers and then the operator. The user entered a number, the operator and another number.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with five function calculator

    Quote Originally Posted by catisch View Post
    I'm sorry, we haven't gotten to parsing or String.split(). That is over my head.
    Sorry. It's hard to tell what's allowed sometimes. If you're also not using try/catch blocks as Norm suggests, then ensuring the Scanner method calls match the user's entry (as Norm suggested) is your next best remedy. Still a pain in the rear and not flexible to handle an uncooperative user, but that's the breaks.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with five function calculator

    @manahil, please read the forum announcements and the following:
    http://www.javaprogrammingforums.com...n-feeding.html

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with five function calculator

    hmmm okay sir! actually i joined first time such kinda forums...and i think it so that,tht person had already put hi best effortto work out with his code...and so i also provide my code which i made 2 days before...so that he can also take my concept,as evry prsn has difrnt programming style... em really unaware of forums rules..i apologize for the inconvenience...!

Similar Threads

  1. Function in Function?
    By junxiqqq in forum Java Theory & Questions
    Replies: 5
    Last Post: May 18th, 2012, 08:05 PM
  2. need help with a function...
    By fallout87 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 2nd, 2011, 05:42 AM
  3. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 09:34 AM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM