Bookmark Topic Watch Topic
  • New Topic

Combination Lock (yes i've looked through all the other posts)  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Report post to moderator
So like I said, I have searched the forums and while there are tons of topics on building a combination lock, I couldn't quite find my answer. Plus all those topics are questions about much more complicated assignments/projects.

So it's relatively simple in my head, but I'm missing something in my public actionPerformed() method. Basically I have a frame with 9 buttons, and if you hit the correct three buttons in the right order the program exits. If your combination guess is wrong, the background changes color to red. So say for 7-3-5; at the point I'm at, I only set the program to exit if the first number is clicked(7), else the background changes color to red. My question is how would I check to make sure the next button click is correct(3 in this case)? I'm thinking it's something simple that I'll kick myself for not realizing, but any help would be appreciated. My current code to exit on the first number is below. Thanks in advance!

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Just for clarification in case my question made no sense, this is the section I can't quite figure out. This is what I'm currently trying and I feel like it's close to correct.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
remember...you will only go into one of the if-block or the else block.

so, if the user enters 7, you are now in the first if-block, and will never hit line 9.

I think you want to take line 9 out of the else block - you always want to hit it - unless you successfully get to line 7.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Report post to moderator
fred rosenberger wrote:remember...you will only go into one of the if-block or the else block.

so, if the user enters 7, you are now in the first if-block, and will never hit line 9.

I think you want to take line 9 out of the else block - you always want to hit it - unless you successfully get to line 7.


hey thank you for the response! so if my original way in the OP worked for the first number:



should I still leave the 3 if statements in my second post the same? only moving the GetContentPane() line elsewhere like you suggested? or is there a better way to check for the 3 numbers in order?
thank you again for the response!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
I would say the answer is:

StopCoding (<---click that)

Don't sit down and start banging out code. Start thinking about the problem and what you want it to do. Until you can explain to a 10yr old child what should happen - and not use java terms - you shouldn't be writing code.

What do you want to happen?

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Report post to moderator
fred rosenberger wrote:I would say the answer is:

StopCoding (<---click that)

Don't sit down and start banging out code. Start thinking about the problem and what you want it to do. Until you can explain to a 10yr old child what should happen - and not use java terms - you shouldn't be writing code.

What do you want to happen?



i've seen it before, and your reply seriously comes off as "elitist". don't ever be a motivational speaker.

i know exactly what i want to happen. i want a user to click three numbers. if they are in the right sequence, then close the program. is that nice enough "10 year old language" for you?

i've had 4 weeks of classes with a bad teacher so you'll have to excuse me not having the entire java documentation memorized as if they were my abc's.

mods you can close this thread.


 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Fred's advise is good. This is all about logic, not Java code. Once you figure out the logic (on paper and pencil, on notepad, in your head, or whatever works for you), only then you should start "translating" each step of the algorithm to Java code.

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Report post to moderator
You need a way to remember what values have been entered in the past, and when you have enough values (or when the user presses some button to check the current values) check them all in one go.

Note that you also have a lot of code duplication. You can create and initialize all your buttons in a single loop. It's also a bad idea to have your frame implement ActionListener. Instead, you can give each button an "anonymous listener" (or a lambda, since Java 8).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Borchgrevink wrote:i know exactly what i want to happen. i want a user to click three numbers. if they are in the right sequence, then close the program. is that nice enough "10 year old language" for you?

Absolutely perfect. Now, if you've really read the page, why didn't you write that down, before you wrote your first line of Java code?
Because if you had, I suspect it might have occurred to you that there are three things that need to happen before you can do your test and therefore, as Stephan said, you need some way of storing those values in order to do it.

i've had 4 weeks of classes with a bad teacher...

Is that our fault? We understand that you might be frustrated, but please don't take yours out on us simply because you're not being handed "answers". That's not how this site works, and getting snippy isn't going to make anyone more inclined to help you.

Fred's absolutely right. You need to write down what needs to be done, in detail, before you start coding; otherwise, the chances are that you'll solve this problem (or, even worse, find some kludge that works this time) and then, an hour later, run into another one, because you haven't thought through the process.

And on a general note: Another problem you have right now is that your problem code is all mixed up with your GUI, so you're actually having to deal with two things at once. If I was trying to emulate a lock, I'd write a Lock (or CombinationLock) class without any GUI code at all that actually models a lock.

Java is an Object-Oriented language, so it usually works best when you have objects working for you. What you have written so far is a procedure.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Borchgrevink wrote:i want a user to click three numbers. if they are in the right sequence, then close the program. is that nice enough "10 year old language" for you?

That's a start. But...what happens after the user clicks the first number vs. the wrong number? Do you even need to ask about the second?
What happens after the user enters the second? and the third?

How do you determine if they are in "the right sequence"?


all that aside...If you think the above is enough, then you need to make your code do that. currently, it doesn't. your description says "the user should click three numbers. After that check to see if they are in the right sequence."

What you code does is "have the user click on number. Check to see if it is right or not. If it is, have them click a second number, otherwise close. If the second number is right, have them enter a third. If it is right, close the program".

Those are two VERY different steps to follow.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Winston Gutkowski wrote:
David Borchgrevink wrote:i know exactly what i want to happen. i want a user to click three numbers. if they are in the right sequence, then close the program. is that nice enough "10 year old language" for you?

Absolutely perfect. Now, if you've really read the page, why didn't you write that down, before you wrote your first line of Java code?
Because if you had, I suspect it might have occurred to you that there are three things that need to happen before you can do your test and therefore, as Stephan said, you need some way of storing those values in order to do it.

i've had 4 weeks of classes with a bad teacher...

Is that our fault? We understand that you might be frustrated, but please don't take yours out on us simply because you're not being handed "answers". That's not how this site works, and getting snippy isn't going to make anyone more inclined to help you.

Fred's absolutely right. You need to write down what needs to be done, in detail, before you start coding; otherwise, the chances are that you'll solve this problem (or, even worse, find some kludge that works this time) and then, an hour later, run into another one, because you haven't thought through the process.

And on a general note: Another problem you have right now is that your problem code is all mixed up with your GUI, so you're actually having to deal with two things at once. If I was trying to emulate a lock, I'd write a Lock (or CombinationLock) class without any GUI code at all that actually models a lock.

Java is an Object-Oriented language, so it usually works best when you have objects working for you. What you have written so far is a procedure.

Winston


yea thanks, that still does nothing to help me. the whole reason i posted this topic was BECAUSE i wasn't sure how to store the previously selected numbers. everything else worked fine and as intended. where once did i ask directly for any "answers"?? i swear, some of you guys get one question and think you're being asked to write the whole program assuming you're guardians of some secret language from intruding aliens. could it POSSIBLY be that there are people on earth that are visual learners? that need to SEE what's happening? god forbid. i wrote the whole thing and got stuck at a single point so i came here to ask a question. maybe i did get "snippy" and that probably wasn't the right reaction, but it doesn't mean i came in here asking somebody else to write the code for me does it? and no, my crappy teacher ISN'T yours or anyone else's fault. i simply stated something that might lead others to believe, "okay, kid needs some help, maybe we're preaching something above his head, let's break it down for him", in hopes that, hey, you know, i might get some help. because i don't know, maybe that's what this site is for??

and please don't respond any more to what i originally asked. another site was more than happy to actually help.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!