• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If causing me some trouble

 
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) I hope that I am posting correctly and not breaking any rules of the board.  If I am let me know. I'm a newbie or fresher as some say.
2) I used a 'code' command on this forum.  I don't know if they are used or if there is another way to do this on the site.
3) This code is supposed to give the user 3 chances to enter an ATM code.  I cannot get it to recognize when the pw is correct.  it does not go in the if condition and I don't know why.

Thanks,
Kevin

 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw another post mentioning to do these items.  Let me add it to my original post.

a simplified example of your code
>> I cant get it much more simple.
What you want it to do
>>Allow 3 attempts for the user to enter the password which I have set as "Alexa".
What it really does
>>It keeps asking for the Password after entering the correct or wrong password.
What you expect it to do.
>>I expected that it would show the statement to the user that they have entered the ATM system.

I hope this improved my post.

Thanks,

Kevin
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you've got some formatting issues with your post, but otherwise it's fine.  I think you have an extra code tag too early.  What I generally do is once I've pasted in my java code, highlight it, then click the "code" formatting button.

As to your issue...

You never want to us "==" to compare anything but primitives (ints, floats, etc).  you really need to use the .equals method...something like



This still may not work, since nextLine may also get the newline at the end...i'm not sure about that...someone may come along soon and correct me on that.
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,
I appreciate the quick response.  I will give the s.equals a try.
Thanks,
Kevin
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You also would use == if either operand is the reserved word null. Note the Java® Language Specification says that == also works if either operand is a constant from an enum. But you probably haven't encountered enums yet. We have an FAQ about ==.
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just as a side note, when comparing strings, it is better to use .equals(). the reason is that string is a class, so sometimes comparing objects with == can give back some wrong answers; therefore using .equals()(defined to compare objects in the java.object class definition) is better than just plain ==
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some folk here would even argue you should do it this way:

password.equals(s)

The reason being that you KNOW, without out a doubt, that "password" is not null.  If somehow your 's' variable doesn't get initialized, you can get a null pointer error by calling s.equals.
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the only problem I see with your code is using == to compare strings in which case you should use .equals() instead to compare string values
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jose Aguilar wrote:. . . . comparing objects with == can give back some wrong answers . . . .

No, == always gives the correct answer, but to the wrong question. If you ask the wrong question, the answer given will not tell you what you need to know. If somebody asks me at 10pm whether I like coffee, the answer is yes. They would then be surprised if I don't drink the coffee they gave me. It is because they asked the wrong question; they should have asked whether I would like some coffee, to which the answer would be, no thank you, it will stop me sleeping.
Asking whether x == y may get the answer no when you actually wanted the answer to whether x.equals(y) which is yes.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:. . . nextLine may also get the newline at the end . . .

Scanner#nextLine always removes the line end characters, but it can return an empty String under some circumstances.
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to reply to a particular post when some one replies to one of my posts?  
I don't see a way to follow a thread.
Thanks,
Kevin
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Is there a way to reply to a particular post when some one replies to one of my posts?

You can click the quote button on an old post, but you would do well to remove most of the quoted text, otherwise the thread simply becomes full of duplications.

I don't see a way to follow a thread.

Have you seen the Watch Topic button at the bottom?

Thanks,
Kevin

That's a pleasure
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just went back to my first post on the Forum listed under "My posts".   Wow! I have been  learning Java for six years.  Even longer since I used it about five years earlier with Selenium.  
It took me 4 months to be very good at BASIC.   After all these years I know a lot of parts of Java but, I'm average at it.  

It's fun or I'd stop.

Kevin      
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic