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

Thread: Code is correct, but incorrect output?

  1. #1
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Code is correct, but incorrect output?

    class div
    {
    public static boolean div(int x, int y)
    {
    boolean pig;
    pig=x%y==0;
    if(pig)
    return pig; 
    else 
    return !pig;
    }
    public static void main(String args[])
    {
    int a,b;
    boolean result;
    a=7;
    b=3;
    result=div(a,b);
    System.out.println(result);
    }
    }

    As far as I can see the code is correct.... But i keep getting the output as true instead of false... Why isn't the complement (!) not working?
    Last edited by helloworld922; February 5th, 2011 at 03:21 PM.


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Code is correct, but incorrect output?

    I don't understand what your trying to accomplish with this code. What do you want the code to do?
    Last edited by sp11k3t3ht3rd; February 5th, 2011 at 03:11 PM. Reason: I'm confused.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code is correct, but incorrect output?

    It looks like div is suppose to return if an integer x is divisible by y?

    Try stepping through the code (i.e. pretend you're the computer).

    1. pig = true or false
    4. if (pig == true)?
    4a. return pig => return true
    5. if (pig == false)?
    5a. return !pig => return !false => return true

    no matter what pig is, you're always returning true.

    Generally you want to create variable names that make sense. x,y, and pig give no indication what that variable is used for. Use a variable name such as numerator, divisor, and isDivisible.

  4. #4
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Code is correct, but incorrect output?

    sorry... My bad... I understand now... Thanks everybody...

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code is correct, but incorrect output?

    not sure if this is correct,

    But I just though U could replace
    return !pig; on line 10 with
    return !true;

    this gives the false return

    IKE

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code is correct, but incorrect output?

    or just use return pig;, no if/else required.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    moodycrab3 (February 7th, 2011)

Similar Threads

  1. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM
  2. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM
  3. It's not printing out the correct percentage...
    By JBow94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2010, 04:07 PM
  4. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM