Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Norm

Thread: Multiple if Statements?

  1. #1
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Default Multiple if Statements?

    So I'm trying to make a simple calculator, just to see if I can:

    Java Code:
    import java.util.Scanner;
    
    class Calculator {
    
    	public static void main(String[] args) {
    		Scanner calculator = new Scanner(System.in);
    		
    		System.out.println("Hello, and welcome to Calculator.");
    		System.out.println("Please type M for multiplication, D for division, S for addition, or A for subtraction.");
    		String firstAnswer = calculator.nextLine();
    		
    		if (firstAnswer.equals("M")) {
    			System.out.println("You have selected multiplication. Please type the first number you wish to multiply.");
    			
    			double multiplyOne = calculator.nextDouble();
    			System.out.println("Please enter your second number.");
    			double multiplyTwo = calculator.nextDouble();
    			double multiplyResult = multiplyOne * multiplyTwo;
    			System.out.print("Your result is ");
    			System.out.print(multiplyResult);
    			
    			calculator.close();
    			System.exit(0);
    	} if (firstAnswer.equals("D")); {
    			System.out.println("You have selected division. Please type the first number you wish to divide.");
    			
    			double divideOne = calculator.nextDouble();
    			System.out.println("Please enter your second number.");
    			double divideTwo = calculator.nextDouble();
    			double divideResult = divideOne / divideTwo;
    			System.out.print("Your result is ");
    			System.out.print(divideResult);
    			
    			calculator.close();
    			System.exit(0);
    			
    	} if (firstAnswer.equals("A")); {
    			System.out.println("You have selected addition. Please type the first number you wish to add.");
    			
    			double addOne = calculator.nextDouble();
    			System.out.println("Please enter your second number.");
    			double addTwo = calculator.nextDouble();
    			double addResult = addOne + addTwo;
    			System.out.print("Your result is ");
    			System.out.print(addResult);
    			
    			calculator.close();
    			System.exit(0);
    
    	} if (firstAnswer.equals("S")); {
    		System.out.println("You have selected subtraction. Please type the first number you wish to subtract.");
    		
    		double subtractOne = calculator.nextDouble();
    		System.out.println("Please enter your second number.");
    		double subtractTwo = calculator.nextDouble();
    		double subtractResult = subtractOne - subtractTwo;
    		System.out.print("Your result is ");
    		System.out.print(subtractResult);
    		
    		calculator.close();
    		System.exit(0);
    		
    	} 
    	
    	}
    
    }
    (I'm probably doing something really stupid) Anyway, for some reason whenever I type anything other than M, it still sends me to division, even though there's no else. What am I doing wrong here?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Multiple if Statements?

    Poorly placed ; in if statements.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    7
    Rep Power
    0

    Default Re: Multiple if Statements?

    Thanks, I can't believe I didn't see that.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Multiple if Statements?

    Also putting a statement after a } is confusing.
    It's better to start a new statement on a new line.
    Also the ending } for a block should be in the same column as the first character on the line that starts the block. For the posted code, the } should be in the same column and below the i in if
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Multiple if Statements?

    I just happened to spot it. I would be nice if the compiler could warn about such things that don't make obvious sense. I vaguely
    remember the PL/1 compiler would warn about a semicolon in comments. And assuming you are new at programming you have redundant code that could be removed. You could do something like this (using pseudo code).

    Java Code:
    if ( some test) {
        answer = ...;
    } else if (another test) {
        answer = ....;
    } else if (another test) {
       answer = ...;
    } else (
        answer = ...;
    }
    Print answer
    Program will exit normally after last statement.

    Regards,
    Jim
    Last edited by jim829; 08-30-2014 at 11:28 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Multiple if Statements?

    The javac compiler issues a warning with the -Xlint option:
    Java Code:
    Running: D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint TestCode18.java
    
    TestCode18.java:553: warning: [empty] empty statement after if
            if( 1 > 2); {
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Multiple if Statements?

    Great! Did not know that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Multiple if statements
    By dojob in forum New To Java
    Replies: 9
    Last Post: 08-02-2013, 05:00 PM
  2. Multiple try & catch statements within a switch
    By Martyn in forum New To Java
    Replies: 21
    Last Post: 01-12-2012, 09:24 PM
  3. Multiple if statements
    By gunnerp420 in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 10:54 PM
  4. how to call multiple sql statements in java
    By sandeep43 in forum JDBC
    Replies: 5
    Last Post: 08-01-2011, 10:13 AM
  5. if statements for multiple variables
    By dookie1293 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 09:30 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
  •