Results 1 to 4 of 4
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Why is my condition statement met? (% operator)

    I can not figure this out. The task is simple, is number1 a multiple of number2 - so I use the % to find out if there is a remainder.

    but look at this

    Java Code:
    package chapter_ii;
    
    import java.util.Scanner;
    
    public class Multiples 
    {
    float number1;
    float number2;
    boolean multiple;
    
    	public static void main (String[] args)
    	{
    		int number1;
    		int number2;
    	
    		Scanner input = new Scanner(System.in);
    	
    		System.out.println("Please input two numbers");
    	
    		number1 = input.nextInt();
    		number2 = input.nextInt();
    	
    		Multiples Multi = new Multiples(number1, number2);
    	
    		Multi.calculate();
    	
    	}
    	
    	public Multiples(int number1, int number2)
    	{
    		this.number1 = (float)number1;
    		this.number2 = (float)number2;
    	}
    	
    	public void calculate()
    	{
    		if ((number1 % number2) == 0.0)
    			multiple = true;
    		else
    			multiple = false;
    		
    		if (multiple = true)
    			System.out.println(number1 + " is a multiple of " + number2);
    		else
    			System.out.println(number2 + " is not a multiple " + number2);
    		
    		System.out.println(number1 + " % " + number2 + " = " + (number1 % number2) );
    		System.out.println(multiple);
    	}
    }
    the output I get is

    Please input two numbers
    5
    2
    5.0 is a multiple of 2.0
    5.0 % 2.0 = 1.0
    true

    whatever I put in I get "true" for the multiple variable... the test I put at the end proves != 0 so why is the condition

    Java Code:
    if ((number1 % number2) == 0.0)
    true every time? I cant not work this out... please help me

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: Why is my condition statement met? (% operator)

    Don't compare two boolean values with the = operator (that's the assignment operator); use the == operator instead. Even better don't compare a boolean value against a constant boolean value.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is my condition statement met? (% operator)

    Quote Originally Posted by JosAH View Post
    Don't compare two boolean values with the = operator (that's the assignment operator); use the == operator instead. Even better don't compare a boolean value against a constant boolean value.

    kind regards,

    Jos
    thank you, I cant believe I missed that - you can see I knew what the equals operator was, because I used it in the first if statement.

    so on my second if statement I wrote if (multiple = true) by mistake

    I'm surprised that even compiled, because if statements arent really statements they're conditionals (doesnt have a semi colon like statements do)... does this mean that when using an if statement you can say if(a = 3) {do this} and it will treat it as a statement and assign 3 then do the task? thats odd I would have imagined a compile error.

    well thats how I learn. thanks I'll try to get better at seeing stuff like this

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: Why is my condition statement met? (% operator)

    Quote Originally Posted by EscSequenceAlpha View Post
    thank you, I cant believe I missed that - you can see I knew what the equals operator was, because I used it in the first if statement.

    so on my second if statement I wrote if (multiple = true) by mistake

    I'm surprised that even compiled, because if statements arent really statements they're conditionals (doesnt have a semi colon like statements do)... does this mean that when using an if statement you can say if(a = 3) {do this} and it will treat it as a statement and assign 3 then do the task? thats odd I would have imagined a compile error.

    well thats how I learn. thanks I'll try to get better at seeing stuff like this
    An if statement takes a boolean epression; an assignment is an expression and in your case (you assigned true to a boolean variable) it is a boolean expression. You can't do if(a = 3) because a = 3 isn't a boolean expression. Check Java's syntax in the JLS for all the gory details.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  2. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  3. Condition ignored.
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 12-09-2011, 03:21 AM
  4. Tenary condition operator
    By mwilliams in forum New To Java
    Replies: 7
    Last Post: 05-05-2011, 11:28 AM
  5. difficulties with IF statement condition
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 10-12-2010, 08:38 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
  •