Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
Like Tree6Likes

Thread: Using if to print words

  1. #21
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Re: Using if to print words

    I'm still curious what error you are getting when you use an if-else statement.

  2. #22
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Using if to print words

    ((RFdoor == 0) ? is the if part.
    these are the true or false parts: "Closed" : "Open"

    so to recap:

    (condition == something) ? truePart : falsePart
    awinston and TANKDS like this.

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Using if to print words

    @.paul. More spoonfeeding code without properly explaining how it works or what its name is so the OP can look it up.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Using if to print words

    @Norm. i explained the code which is an inline if statement. if you have a better explanation, go ahead + explain it.
    i'm not seeing many useful answers here. no code, no explanations, just vague hints.

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Using if to print words

    Its called a ternary operator. See Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    and google for more descriptions of how to use it.

    Many of us here don't think giving an OP the code for his problem without a good explanation of how it works is the right way to teach programming.
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    For this situation, the ternary operator is probably the best. However, it's still important that OP understands how to use an if-else statement.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #27
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Using if to print words

    Quote Originally Posted by .paul. View Post
    i'm not seeing many useful answers here. no code, no explanations, just vague hints.
    Vague hints *are* useful answers. Spoonfeeding code rarely helps someone to learn anything at all.

    I appreciate that TANKDS continued with the thread and enhanced his/her own understanding. When you've been around forums as long as some of us, you'll know that the vast majority of questioners vanish from a thread the moment they have a code that works -- occasionally, freebies posted by a member as new to forums as you are -- and reappear with a very similar problem in days or weeks.

    If you're fond of solving other people's problems without concern for furthering their understanding and knowledge, I suggest you try StackOverflow, where credit is given for bare-bones solutions. Here, as in other forums I frequent, we're more concerned in teaching people how to fish than in giving them a free fish.

    db
    awinston likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #28
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by DarrylBurke View Post
    Vague hints *are* useful answers. Spoonfeeding code rarely helps someone to learn anything at all.

    I appreciate that TANKDS continued with the thread and enhanced his/her own understanding. When you've been around forums as long as some of us, you'll know that the vast majority of questioners vanish from a thread the moment they have a code that works -- occasionally, freebies posted by a member as new to forums as you are -- and reappear with a very similar problem in days or weeks.

    If you're fond of solving other people's problems without concern for furthering their understanding and knowledge, I suggest you try StackOverflow, where credit is given for bare-bones solutions. Here, as in other forums I frequent, we're more concerned in teaching people how to fish than in giving them a free fish.

    db
    To be fair, .paul did 2 things right:

    1. He introduced a concept into the conversation that was previously unmentioned. This concept happens to be a pretty good solution for OP's solution.

    2. He provided an explanation that, depending on OP's skill level, sheds light on the ternary operator.

    Criticizing the hints given to OP was not good, however.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  9. #29
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    I understand Paul's explanation quite well and I am glad that he made the input he did as it allowed me to solve my problem, and I have written a completely different java file to help me understand the if and else if. The only thing I didn't understand was when I compiled it I had errors so I replaced all my " with ' and the code ran fine. Could someone explain the difference of using an apostrophe and speech marks in my code? Notepad++ marked both of them as grey and showed them to be the same thing but when compiled " returned errors and ' compiled with no problems.

  10. #30
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by TANKDS View Post
    I understand Paul's explanation quite well and I am glad that he made the input he did as it allowed me to solve my problem, and I have written a completely different java file to help me understand the if and else if. The only thing I didn't understand was when I compiled it I had errors so I replaced all my " with ' and the code ran fine. Could someone explain the difference of using an apostrophe and speech marks in my code? Notepad++ marked both of them as grey and showed them to be the same thing but when compiled " returned errors and ' compiled with no problems.
    " is used to designate a literal String.

    ' is used to designate a character.

    I'm not sure why using String would produce an error for you.
    TANKDS likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  11. #31
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    It was when I was you the if statement to say that if number > 90 then it would be an A and for some reason which I couldn't work out at the time using " returned errors but using ' did not, thanks for clearing that up .:)

  12. #32
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Using if to print words

    That's because char is a numeric type; String isn't. You can't use mathematical operators like < > with Strings.

    db
    awinston likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #33
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Using if to print words

    Are you familiar with the ternary operator?

    So, you usually start with some kind of assignment:
    Java Code:
    String str =
    and then you ask a question
    Java Code:
    String str = someNumber < 3 ?
    ... and if the answer to the question is yes, then you return some value:
    Java Code:
    String str = someNumber < 3 ? "Robert"
    ... but if the answer to the question is no, then we return a different value:
    Java Code:
    String str = someNumber < 3 ? "Robert" : "Mark";
    Does that help explain it?

  14. #34
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by quad64bit View Post
    Are you familiar with the ternary operator?

    So, you usually start with some kind of assignment:
    Java Code:
    String str =

    and then you ask a question
    Java Code:
    String str = someNumber < 3 ?
    ... and if the answer to the question is yes, then you return some value:
    Java Code:
    String str = someNumber < 3 ? "Robert"
    ... but if the answer to the question is no, then we return a different value:
    Java Code:
    String str = someNumber < 3 ? "Robert" : "Mark";
    Does that help explain it?
    The syntax makes more sense to me, now. Thanks!
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  2. Need to find a way to print line spacing by words
    By technoreject in forum New To Java
    Replies: 1
    Last Post: 07-07-2011, 02:43 PM
  3. Replies: 3
    Last Post: 02-13-2011, 08:54 PM
  4. How to print words in different colours?
    By veen in forum New To Java
    Replies: 7
    Last Post: 06-10-2010, 04:54 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

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
  •