• Post Reply Bookmark Topic Watch Topic
  • New Topic

digital combination lock problem  RSS feed

 
N Frank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I'm very new to java and this is my first time posting here. My problem is that I need to write a program that is a digital combination lock three number buttons and a text field, and a reset button (no problems there) my problem occurs when trying to input the numbers via the buttons (I can write a program where the combination is input via a text field). The chapter this problem is in is title "selection" and introduces "if" "else" "switch" etc. I'm not sure how to get the program to recognize which buttons are pressed when. I tried to declare variables as boolean and this worked a bit better but the numbers could be pressed in any order and the "lock" would display unlocked. The second part of the question involves enabling the user to set their own code, but one step at a time. I'll post my code so far (the neatest version). Also this is not homework but I am reading ahead for the fall semester, and my first computer science class. I'm not sure how to post the code in the correct way but I'll try.



 
N Frank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that the && is not doing what I want but am a bit stuck here.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically your code is saying if the event of the source is button1 and button2 and button3 then unlock. Which is not possible. You probably want to use or (||). So if you press one button 1,2 or 3 unlock else set empty text.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the JavaRanch!
 
N Frank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi and thanks, to clarify I need to input a 3 digit code say 231 via the buttons, I tried || but no help sort of a twobutton then threebutton then onebutton idea. I'll give the chapter another look through and see if I missed something.

I'm sure I'll be here fairly often as the new school year begins.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

With your if-clause in the "action performed"method , you are actually testing if all of the 3 Buttons are pressed at the same time.
(Im not sure this is actually possible to happen)

If I understand your intent correctly, you want to test if they are pressed in the right order.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, Jon Avadis, and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not like anything involving addActionListener(this). I think you ought to add different ActionListeners to the different buttons. I haven't written about that very often; I often refer to my old threads, however.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Agree, Jon Avadis, and welcome to the Ranch


Thanks Champ.. eh Camp Nice to be here.

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!