Results 21 to 34 of 34
Thread: Using if to print words
- 08-11-2012, 12:57 AM #21
-
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
- 08-11-2012, 01:49 AM #23
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.
-
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.
- 08-11-2012, 02:04 AM #25
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.
- 08-11-2012, 02:25 AM #26
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-11-2012, 07:11 AM #27
Re: Using if to print words
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-11-2012, 07:20 AM #28
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Using if to print words
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
- 08-11-2012, 02:59 PM #29
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
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.
- 08-11-2012, 04:31 PM #30
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Using if to print words
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-11-2012, 04:44 PM #31
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
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 .:)
- 08-12-2012, 09:41 AM #32
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-14-2012, 05:16 PM #33
Re: Using if to print words
Are you familiar with the ternary operator?
So, you usually start with some kind of assignment:
and then you ask a questionJava Code:String str =
... and if the answer to the question is yes, then you return some value:Java Code:String str = someNumber < 3 ?
... but if the answer to the question is no, then we return a different value:Java Code:String str = someNumber < 3 ? "Robert"
Does that help explain it?Java Code:String str = someNumber < 3 ? "Robert" : "Mark";
- 08-14-2012, 05:22 PM #34
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Similar Threads
-
Javax Print Attribute for Selection Print Range
By rsawatzky in forum AWT / SwingReplies: 0Last Post: 04-26-2012, 12:14 AM -
Need to find a way to print line spacing by words
By technoreject in forum New To JavaReplies: 1Last Post: 07-07-2011, 02:43 PM -
About how to just print last 20 words from a input file
By Holic in forum New To JavaReplies: 3Last Post: 02-13-2011, 08:54 PM -
How to print words in different colours?
By veen in forum New To JavaReplies: 7Last Post: 06-10-2010, 04:54 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


6Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks