RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Practice 'til your hands twitch.
Jesse Silverman wrote:What is the if condition in your if-else-if set?
Jesse Silverman wrote:Why is it hard-coded to be the same in all of the clauses?
if the first one matches, they all will.
If the first one doesn't match, none will.
Campbell Ritchie wrote:But you always know there will be a first page unless your text is length 0. What do your selection statements in lines 31‑38 do?
Farakh khan wrote:
Campbell Ritchie wrote:But you always know there will be a first page unless your text is length 0. What do your selection statements in lines 31‑38 do?
With due respect I am already not able to fix my posted question. So not able to answer more questions. If you know anything that could be helpful then please post it. I don't know you are answering my question or adding more questions to me ):
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:
Farakh khan wrote:
Campbell Ritchie wrote:But you always know there will be a first page unless your text is length 0. What do your selection statements in lines 31‑38 do?
With due respect I am already not able to fix my posted question. So not able to answer more questions. If you know anything that could be helpful then please post it. I don't know you are answering my question or adding more questions to me ):
OK. I agree with the others, but let's see if I can get you to see it.
Because currentPage does NOT change inside your for loop, the code you have that says:
does the same thing in each test. If the first one matches, the others never get executed.
If the first one doesn't match, neither will the next two.
The smallest change you could make would be to explicitly specify the page numbers in your if statements, instead of saying currentPage, so they are actually different.
While you could make that work if you put the right stuff in the { } braces after the tests, I agree that it would be more normal, and neater, to just create an array of Strings and to then refer to the members of that array by index. What if later you have eight or nine or ten different possible pages? The logic as you have it would be fine if there were ever just one or two, but scales poorly when the number of possible pages goes higher.
However, the if tests that you have coded don't even work to pick from one of two or three pages, because the test you are doing in each one, f(pageNo == currentPage) is exactly the same in each.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:Let's back up even further.
Do you want that this code prints out exactly one of the Strings, or all of them in succession?
If you want to print out only one, there is no need at all for a for loop, as Paul stated.
If you mean to print out the first one, then the second one, then the third one, you can either place them into an array and print the 1st, 2nd, 3rd (and maybe the 40th) inside a simple for loop with indexing, or you could continue your approach (which scales poorly to more strings) by fixing the if statements and placing the print() calls inside the { } code for each if else statement.
Jesse Silverman wrote:Do you understand the following code pasted from my jshell?
Wouldn't something similar help you in your problem?
jshell> String s1 = "Huey";
s1 ==> "Huey"
jshell> String s2 = "Dewey";
s2 ==> "Dewey"
jshell> String s3 = "Louie";
s3 ==> "Louie"
jshell> String[] pages = {s1, s2, s3};
pages ==> String[3] { "Huey", "Dewey", "Louie" }
jshell> System.out.println( pages[1] );
Dewey
Don't get me started about those stupid light bulbs. |