Tim Holloway wrote:"optimization" is not the problem. Or rather, Java Strings are not the optimal way to express HTML. Which is why we also have JSPs.
Tim Holloway wrote:
Jesse Silverman wrote:
I feel my lip curling. No, this is really ugly code that could be made a lot more maintainable. It looks like something a beginner would write before learning better, in fact.
Yes, it's nicer to have everything in one file, but sometimes "nicer" isn't better.
I confess its very ugly code. I tried as under to optimize it:
Here is the problem which I am trying to fix it:
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
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:
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.
Junilu Lacar wrote:
Farakh khan wrote:Hello,
Here is my code:
I hear a phone ringing... I bet it's the late 1990s calling to say "You need to stop doing that!"
Tim Holloway wrote:We spend a lot of time discouraging people from trying to write complex application logic on JSPs using scriptlets.
Tim Holloway wrote:
Here, however. you've transgressed in the opposite direction.
Your life will become much easier if you use the popular servlet+JSP architecture where a servlet handles the business logic, collects the results in JavaBeans, then forwards to a JSP that uses the JavaBeans to populate the display and - in conjunctions with JSTL tags, to determine what blocks of HTML to show or hide based on the bean values.
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?