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: Hi I have a problem with my calculator

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi I have a problem with my calculator

    Here is the code for my simple calculator:

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
    package calculator_1;

    import java.util.Scanner;

    public class apples {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Calculator");

    System.out.println("Enter a number");
    int number1 = scan.nextInt();


    System.out.println("Enter a number");
    int number2 = scan.nextInt();


    // doing math with numbers
    int add = number1 + number2;
    int sub = number1 - number2;
    int times = number1 * number2;
    int divide = number1 / number2;

    //choosing operation to use, if statements.
    System.out.println("What operation do you want to use?: add, sub, times, divide");
    Scanner opy = new Scanner(System.in);
    String operator = opy.nextLine();

    if (operator == "add")
    {
    System.out.println(" Add: " + add);
    }
    if (operator == "sub")
    {
    System.out.println(" Sub: " + sub);
    }

    if (operator == "times")
    {
    System.out.println(" Times: " + times);
    }
    if (operator == "divide")
    {
    System.out.println(" Divide: " + divide);
    }



    }

    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>

    My code seems to work with allowing me to enter my two numbers but the problems exist when it asks me what order of operation I want to apply to my two numbers with the prompt working but when I enter my response like "add" for example, nothing happens, please help.


    Thanks for your help
    Jesse....


  2. #2
    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: Hi I have a problem with my calculator

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

    Do not use '==' to compare Strings. Instead, use the equals() method: if "thisString".equals( "thatString" );

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi I have a problem with my calculator

    Sorry for the incorrect code convention and thanks for answering my question now that part seems to be solved I have run into another problem to do with the if statements which when I type "add" for example after the prompt ask's me what operation I want it displays all of the operations. My code is here:

    package calculator_1;
     
    import java.util.Scanner;
     
    public class apples {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Calculator");
     
    		System.out.println("Enter a number");
    		int number1 = scan.nextInt();
     
     
    		System.out.println("Enter a number");
    		int number2 = scan.nextInt();
     
     
    		// doing math with numbers
    				int add = number1 + number2;
    				int sub = number1 - number2;
    				int times = number1 * number2;
    				int divide = number1 / number2;
     
    				//choosing operation to use, if statements.
    		System.out.println("What operation do you want to use?: add, sub, times, divide");
    		Scanner opy = new Scanner(System.in);
    		String operator = opy.nextLine();
     
    		if (operator.equals( "add" )); 
    		{
    			System.out.println(" Add: " + add);
    		}
    		 if (operator.equals( "sub" ));
    		{
    		System.out.println(" Sub: " + sub);
    		}
     
    		 if (operator.equals( "times" ));
    		{
    			System.out.println(" Times: " + times);
    		}
    		 if (operator.equals ( "divide" ));
    		{
    			System.out.println(" Divide: " + divide);
    		}
     
     
     
    	}

  4. #4
    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: Hi I have a problem with my calculator

    Semicolons at the end of your if() statements complete the clauses so that the intended bodies of the if() statements are not connected to the if() statement conditions and will execute regardless. Remove the semicolons at the ends of your if() statements.

Similar Threads

  1. Problem with calculator in awt
    By R.K in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 10th, 2014, 01:43 PM
  2. problem with calculator
    By alexd in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 6th, 2014, 07:18 PM
  3. [SOLVED] Another Calculator Problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 4th, 2010, 03:31 PM
  4. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM

Tags for this Thread