Results 1 to 18 of 18
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Question concerning question marks and colons

    Would someone please be kind enough to explain the purpose of the question mark and colon in this program? Thank you.

    Java Code:
    import java.util.Scanner;
    
    public class DivisibleFiveOr6 {
    	public static void main(String [] args) {
    
    		//declare variable
    		int count = 1;
    
    		//processing and output
    		for (int i = 100; i <= 1000; i++)
    			if (i % 5 == 0 && i % 6 == 0)
    		    	System.out.print((count++ % 10 != 0) ? i + " ": i + "\n");
    		  }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    That's the ternary operator. Google is your friend.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    I wouldn't have asked the question if I hadn't already Googled "Java question mark" and "java colon." What I read did nothing but confuse me so I thought if I posted an example someone might be nice enough to use that example to give me a specific explanation.

    If that offends your delicate sensibilities then feel free to piss off.

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    After further reflection, I realize that I did not explain myself well enough in my original post. There was no way for anyone to know that I had already researched before asking my question, thus opening myself up to the response I received.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    Okay, googling "java question mark" and "java colon" is different from googling "java ternary operator" which is what I hinted you do.

    ignoreList++

  6. #6
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Okay, googling "java question mark" and "java colon" is different from googling "java ternary operator" which is what I hinted you do.
    No, actually it's not. I get pretty much the same sites regardless.

    Quote Originally Posted by KevinWorkman View Post
    Ignore++
    Works for me. I came here to learn not receive smart alec comments.

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

    Default

    Quote Originally Posted by jim01 View Post
    No, actually it's not. I get pretty much the same sites regardless.
    Googling for "Java question mark colon" works for me ...

    kind regards,

    Jos

    ps. As does "Java ternary operator".
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Okay, googling "java question mark" and "java colon" is different from googling "java ternary operator" which is what I hinted you do.

    ignoreList++
    Quote Originally Posted by JosAH View Post
    Googling for "Java question mark colon" works for me ...

    kind regards,

    Jos

    ps. As does "Java ternary operator".
    Yes sir, either way you Google it you get the same information. My point was that I did not ask my question without researching it first. As I stated, what I read was confusing to me. The examples given all look something like this:

    minVal = (a < b) ? a : b;

    How I read this is (and per one of the sites I visited) if the variable a is less than b, minVal is assigned the value of a; otherwise, minVal is assigned the value of b.

    However, this is not how it is done in the program that I asked about. There is no equal sign. This is where my confusion comes in. Instead of (a < b) I have (count++ % 10 != 0) and instead of a : b; I have i + " ": i + "\n". So where is the equivalent of the minVal? Is it System.out.println? Is it the if statement, or is it the while statement?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Ah, there may not be an equals sign, but there is an assignment.

    The result of the ternary operator is assigned to the parameter for the println() method...:)

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

    Default

    Quote Originally Posted by jim01 View Post
    Yes sir, either way you Google it you get the same information. My point was that I did not ask my question without researching it first. As I stated, what I read was confusing to me. The examples given all look something like this:

    minVal = (a < b) ? a : b;

    How I read this is (and per one of the sites I visited) if the variable a is less than b, minVal is assigned the value of a; otherwise, minVal is assigned the value of b.

    However, this is not how it is done in the program that I asked about. There is no equal sign. This is where my confusion comes in. Instead of (a < b) I have (count++ % 10 != 0) and instead of a : b; I have i + " ": i + "\n". So where is the equivalent of the minVal? Is it System.out.println? Is it the if statement, or is it the while statement?
    The general (syntactic) structure of the ternary operator is:

    Java Code:
    <boolean-expression>?<true-value>:<false-value>
    If <boolean-expression is true the value of the entire ternary expression is <true-value>; otherwise it's <false-value>, so for the expression:

    Java Code:
    (count++ % 10 != 0) ? i + " ": i + "\n"
    the <boolean-expression> is (count++%10 != 0). If it is true the value of the entire ternary expression is i+" ", otherwise it is i+"\n". The result of the entire expression (one of <true-value> or <false-value> is printed. It is a bit of a convoluted expression but it takes care that a space is printed after every value of i, except for every tenth value, a newline is printed after the value of i. The output (sort of) ends up in columns.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default

    This is the second result for googling "java ternary operator": Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    It contains the following:

    Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

    The following program, ConditionalDemo2, tests the ?: operator:

    class ConditionalDemo2 {

    public static void main(String[] args){
    int value1 = 1;
    int value2 = 2;
    int result;
    boolean someCondition = true;
    result = someCondition ? value1 : value2;

    System.out.println(result);

    }
    }

    Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

  12. #12
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Ah, there may not be an equals sign, but there is an assignment.

    The result of the ternary operator is assigned to the parameter for the println() method...:)
    OK so it's println. Thank you. :)

  13. #13
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    the <boolean-expression> is (count++%10 != 0). If it is true the value of the entire ternary expression is i+" ", otherwise it is i+"\n". The result of the entire expression (one of <true-value> or <false-value> is printed. It is a bit of a convoluted expression but it takes care that a space is printed after every value of i, except for every tenth value, a newline is printed after the value of i. The output (sort of) ends up in columns.

    kind regards,

    Jos
    Absolutely beautiful. That explains it perfectly for a thickheaded person such as myself. Thank you very much.

  14. #14
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    This is the second result for googling "java ternary operator": Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    It contains the following:

    Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

    The following program, ConditionalDemo2, tests the ?: operator:

    class ConditionalDemo2 {

    public static void main(String[] args){
    int value1 = 1;
    int value2 = 2;
    int result;
    boolean someCondition = true;
    result = someCondition ? value1 : value2;

    System.out.println(result);

    }
    }

    Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).
    While I have no doubt that the above made complete sense to you, it did nothing but confuse me. I mean it made sense as far as the example went, but I was unable to transfer the understanding of the example to make the program I posted make sense. I apologize for my thick-headedness and thank you for taking the time to explain it to me.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by jim01 View Post
    OK so it's println. Thank you. :)
    Looking back at the code you posted it's actually a print(), but the concept still stands.
    :)

  16. #16
    sawadee is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default

    Please can anyone explain this line to me:

    (box1.level > box2.value ? -1 : (box1.value == box2.value ? 0 : 1));

    To rewrite this line in a simple if statement condition, i know that it starts like this:

    if (box1.level > box2.level){
    // do not understand
    }
    else {
    // do not understand
    }

    the statement in side if and else condition - i'm so blank.

    thanks

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

    Default

    What you have is a nested ternary operator. If the result of the first ternary operator is false then it evaluates the second ternary operator. since ternary operators have been sufficiently coveread already in this thread you should be able to write the code yourself.

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

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. JDO Question...
    By Kam in forum JDBC
    Replies: 0
    Last Post: 01-07-2010, 06:11 PM
  3. Marks programm
    By blackmoon in forum AWT / Swing
    Replies: 5
    Last Post: 12-08-2009, 10:06 PM
  4. Retrieving double quotation marks from xml using java
    By fireportal in forum New To Java
    Replies: 3
    Last Post: 11-12-2009, 11:13 AM
  5. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 09:17 PM

Posting Permissions

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