• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

is equal operator not working in if statement

 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Here is my code:
<br /> <br /> I want that when pageNo == currentPage then show its relevant string but not working <br /> Here is the output: <br />

Why this code output pageNo as 2 at start of if statement whereas it should be 1

Thanks in anticipation
 
Jesse Silverman
Ranch Foreman
Posts: 408
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the if condition in your if-else-if set?
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.

Did you follow thru the loop in any debugger to see what was going on?
 
Elya Matsunomi
Greenhorn
Posts: 25
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code has lots of escape sequence errors. I fixed the errors in firstPage. Just do the same with the other pages

 
Paul Clapham
Marshal
Posts: 26598
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a remarkably convoluted way of choosing an element from an array of three elements, so it's not really surprising that it doesn't do what you want.

You don't need a loop at all. Just this:



copied three times with appropriate changes. But I'd still just use a String array and just choose the appropriate element to print. No ugly logic required then.
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:What is the if condition in your if-else-if set?



I am trying to display first, second and third page string according to the current page e.g. if currentPage is 2 then show secondPage string

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.



You are right but my mind is not working how I fix it. Please help me
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham: Thanks for your reply but I don't know in Pagination how many pages will be there that's why I not hard coded as
 
Campbell Ritchie
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ):
 
Jesse Silverman
Ranch Foreman
Posts: 408
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.






Obviously the following will work:

I am looking for as to how can I control it dynamically?




 
Jesse Silverman
Ranch Foreman
Posts: 408
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Foreman
Posts: 408
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thanks for your favorable replies


This is working fine but let me understand if I say:

I am thinking that in first loop if statement pageNo should be 1 and currentPage should be 2 and should not be run but unfortunately its printing and ignoring other statemetns. Why? I want to understand the logic as to why its not going to the next else if statement where pagrNo is 2 and its equal to currentPage
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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





Yes you are right

This is working as I was intended

Thanks from the bottom of my heart
 
Farakh khan
Ranch Hand
Posts: 849
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I mark this answer as correct one?
 
Rohit Nishad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"is equal operator not working in if statement"
of course not it works.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic