Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: finding errors in if statements

  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default finding errors in if statements

    Find the errors in the following if statements.
    a. if (x && y == 0) { x = 1; y = 1; }
    b. if (1 <= x <= 10)
    System.out.println(x);
    c. if (!s.equals("nickels") || !s.equals("pennies")
    || !s.equals("dimes") || !s.equals("quarters"))
    System.out.print("Input error!");
    d. if (input.equalsIgnoreCase("N") || "NO")
    return;


    To some advanced users, this may seem simple. However for me it;s hard to pick out! Does anyone want to take a wack at it? Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: finding errors in if statements

    Quote Originally Posted by JMAsterson View Post
    Find the errors in the following if statements.
    a. if (x && y == 0) { x = 1; y = 1; }
    b. if (1 <= x <= 10)
    System.out.println(x);
    c. if (!s.equals("nickels") || !s.equals("pennies")
    || !s.equals("dimes") || !s.equals("quarters"))
    System.out.print("Input error!");
    d. if (input.equalsIgnoreCase("N") || "NO")
    return;


    To some advanced users, this may seem simple. However for me it;s hard to pick out! Does anyone want to take a wack at it? Thanks!
    The first person who should publish a "whack at it" in this thread is you.

    Please elevate this thread out of the realm of "homework dump" and into the realm of "collaborative effort".
    Norm likes this.

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: finding errors in if statements

    Ok, not sure if this is right. I think in b.) it should be changed to f (1 <= x && x <= 10) otherwise it wont work.
    In c.) ! means not, so I dont understand what !s.equals("dimes") etc means? that looks like it would translate to not s equals.
    a.) doesnt make much sense to me either because what i'm getting from it is : if x and y have a value of 0, x =1 and y=1.

  4. #4
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: finding errors in if statements

    Can someone tell me if my answer (or part of the answer) if true at all? Thanks.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: finding errors in if statements

    c) is also incorrect but for an entirely different reason (the others don't even syntactically make sense); suppose you want to test something against A and B; suppose that something is C; of course it isn't equals to A or B; but now suppose the something if A; it is definitely equal to A but it isn't equal to B so the or test (||) is still true: A isn't equals to A or B.

    kind regards,

    Jos (or not Jos)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: finding errors in if statements

    Oh, i see, yes that makes sense. So are you saying my other answers are wrong? Not sure where else to go from here with that.

Similar Threads

  1. Finding Errors in JSP page with PMD
    By srinivasmallabathula in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-21-2011, 03:29 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. Replies: 12
    Last Post: 05-06-2010, 11:54 AM
  4. Pleas help on finding errors
    By McXxT in forum New To Java
    Replies: 6
    Last Post: 04-02-2009, 09:42 AM
  5. Replies: 3
    Last Post: 03-08-2009, 02:51 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
  •