Results 1 to 17 of 17
  1. #1
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Using boolean without an if statement?

    So my college professor is asking us to write a program that will end with the program asking for confirmation that it works, i.e. "was the program successful?" and we would type in "yes" (with the program ignoring casing) and that would register in the boolean as "true" and the program would respond to "yes" by printing out "true", while in any other scenario it would print out "false". He also says that we can't use an if statement to do this.

    Now, I'm probably missing something obvious here, but for all the online resources at my fingertips, everything is telling me I need an if statement. Is there anyway I can do this without an if statement?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    You could use a ternary operator.

  3. #3
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    String ans = new String("answer");
    boolean answer = true;
    ans = answer ? "yes" : "no";
    ans = scan.next();
    System.out.println(answer);

    I'm probably not using that right, but it technically works. The only problem is I get "true" no matter what I type.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    That is because you are not using it correctly. The part before the ? is the condition. If it evaluates to true then it returns the first argument after the ?, it if is false it will return the second argument. When will the condition ever be false?

  5. #5
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    When the user inputs anything other than "yes"

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    Where in your code do you compare user input to anything?

  7. #7
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    It's just supposed to be a yes or no question that the code will identify as true or false. If the user types yes, it would say true. False for anything else. That's what I'm trying to figure out. I don't know how to compare user input.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    But how do you know if they typed yes or no??????

  9. #9
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    That's what I'm asking! How does the code identify "yes" or "no" when it gets "yes" or "no" from the user?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    I told you, use the ternary operator. OK lets try this from a different angle. Can you write the code using an if statement? Post it here using code tags.

  11. #11
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    We're not allowed to use an if statement. That's the problem.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    :headdesk:
    :headdesk:
    :headdesk:

    Just write the code using the if so I can see if you understand how it can be done. Once you have done that we can work on the ternary operator version.

  13. #13
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    Ok. And how do I do that with strings? I know how with primitives, but not strings.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    Look at the methods of the String class.

  15. #15
    magichaxx is offline Member
    Join Date
    Nov 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using boolean without an if statement?

    Ok, this assignment is due in less than 3 hours and this isn't my only problem. I'm about 10 seconds away from kirking it but thanks for trying help anyway.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Using boolean without an if statement?

    You don't need if statements to determine if something is true or false. You can assign conditional statements to boolean variables. The value of the variable will be the result of the conditional.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Using boolean without an if statement?

    D'oh!

    I was too busy trying to replace if with ternary operator that I didn't realise that all than needs to be done is a call to a String method.

Similar Threads

  1. Replies: 5
    Last Post: 07-12-2013, 01:22 AM
  2. boolean method return statement error
    By Army in forum New To Java
    Replies: 8
    Last Post: 12-10-2012, 06:54 PM
  3. boolean isDigit() inside if statement
    By Googol in forum New To Java
    Replies: 2
    Last Post: 08-03-2012, 08:22 AM
  4. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  5. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •