Java. Trying to adjust the attempts a user has to unlock a lock in a program and finalizing program












-2















import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));

System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);

Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false

lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close();
lock1.close();

}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;

Lock(int key)
{
isOpen = false;
this.key = key;
}

public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}


as of right now, my program only gives me two attempts and then is gives me an output of false when I want it to say ALARM. Edit the code if you like and comment exactly what I should do. I also have some more debugging that would be nice if I got some help with so that I can have a fully functional program










share|improve this question

























  • You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

    – flakes
    Nov 22 '18 at 2:56











  • Why are you expecting it to print "ALARM" if you've only made 2 attempts?

    – shmosel
    Nov 22 '18 at 2:59
















-2















import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));

System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);

Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false

lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close();
lock1.close();

}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;

Lock(int key)
{
isOpen = false;
this.key = key;
}

public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}


as of right now, my program only gives me two attempts and then is gives me an output of false when I want it to say ALARM. Edit the code if you like and comment exactly what I should do. I also have some more debugging that would be nice if I got some help with so that I can have a fully functional program










share|improve this question

























  • You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

    – flakes
    Nov 22 '18 at 2:56











  • Why are you expecting it to print "ALARM" if you've only made 2 attempts?

    – shmosel
    Nov 22 '18 at 2:59














-2












-2








-2








import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));

System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);

Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false

lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close();
lock1.close();

}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;

Lock(int key)
{
isOpen = false;
this.key = key;
}

public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}


as of right now, my program only gives me two attempts and then is gives me an output of false when I want it to say ALARM. Edit the code if you like and comment exactly what I should do. I also have some more debugging that would be nice if I got some help with so that I can have a fully functional program










share|improve this question
















import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));

System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);

Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false

lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close();
lock1.close();

}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;

Lock(int key)
{
isOpen = false;
this.key = key;
}

public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}


as of right now, my program only gives me two attempts and then is gives me an output of false when I want it to say ALARM. Edit the code if you like and comment exactly what I should do. I also have some more debugging that would be nice if I got some help with so that I can have a fully functional program







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 2:57







Joshua Gentile

















asked Nov 22 '18 at 2:49









Joshua GentileJoshua Gentile

54




54













  • You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

    – flakes
    Nov 22 '18 at 2:56











  • Why are you expecting it to print "ALARM" if you've only made 2 attempts?

    – shmosel
    Nov 22 '18 at 2:59



















  • You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

    – flakes
    Nov 22 '18 at 2:56











  • Why are you expecting it to print "ALARM" if you've only made 2 attempts?

    – shmosel
    Nov 22 '18 at 2:59

















You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

– flakes
Nov 22 '18 at 2:56





You need to learn how to use a debugger and step through your code one line at a time to see whats actually happening. You can find guides on google or youtube for doing this with Eclipse or Intellij or whatever IDE you're using.

– flakes
Nov 22 '18 at 2:56













Why are you expecting it to print "ALARM" if you've only made 2 attempts?

– shmosel
Nov 22 '18 at 2:59





Why are you expecting it to print "ALARM" if you've only made 2 attempts?

– shmosel
Nov 22 '18 at 2:59












2 Answers
2






active

oldest

votes


















0














You aren't getting two attempts right now. What you are seeing currently is the output from this set of code:



System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false


You currently only have one open attempt for each lock. If you add more open attempts like:



lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2


It will print out your expected behaviour






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











  • My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

    – M.G
    Nov 22 '18 at 3:50





















0














If I understand it right, you want to the Locks to be OPEN by default? If so, just change to this:



Lock(int key) 
{
isOpen = true; // < true as default (instead of false)
this.key = key;
}


This leads to the following output:




  • true

  • Opened

  • Opened






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423199%2fjava-trying-to-adjust-the-attempts-a-user-has-to-unlock-a-lock-in-a-program-and%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You aren't getting two attempts right now. What you are seeing currently is the output from this set of code:



System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false


You currently only have one open attempt for each lock. If you add more open attempts like:



lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2


It will print out your expected behaviour






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











  • My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

    – M.G
    Nov 22 '18 at 3:50


















0














You aren't getting two attempts right now. What you are seeing currently is the output from this set of code:



System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false


You currently only have one open attempt for each lock. If you add more open attempts like:



lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2


It will print out your expected behaviour






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











  • My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

    – M.G
    Nov 22 '18 at 3:50
















0












0








0







You aren't getting two attempts right now. What you are seeing currently is the output from this set of code:



System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false


You currently only have one open attempt for each lock. If you add more open attempts like:



lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2


It will print out your expected behaviour






share|improve this answer













You aren't getting two attempts right now. What you are seeing currently is the output from this set of code:



System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111

System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222

Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);

System.out.println(lock1.isOpen); // prints false


You currently only have one open attempt for each lock. If you add more open attempts like:



lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2


It will print out your expected behaviour







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 3:02









M.GM.G

388310




388310













  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











  • My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

    – M.G
    Nov 22 '18 at 3:50





















  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40











  • My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

    – M.G
    Nov 22 '18 at 3:50



















Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

– Joshua Gentile
Nov 22 '18 at 3:40





Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

– Joshua Gentile
Nov 22 '18 at 3:40













My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

– M.G
Nov 22 '18 at 3:50







My answer addresses your problem. As I said you aren't attempting to open the locks three times in your code right now.

– M.G
Nov 22 '18 at 3:50















0














If I understand it right, you want to the Locks to be OPEN by default? If so, just change to this:



Lock(int key) 
{
isOpen = true; // < true as default (instead of false)
this.key = key;
}


This leads to the following output:




  • true

  • Opened

  • Opened






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40
















0














If I understand it right, you want to the Locks to be OPEN by default? If so, just change to this:



Lock(int key) 
{
isOpen = true; // < true as default (instead of false)
this.key = key;
}


This leads to the following output:




  • true

  • Opened

  • Opened






share|improve this answer
























  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40














0












0








0







If I understand it right, you want to the Locks to be OPEN by default? If so, just change to this:



Lock(int key) 
{
isOpen = true; // < true as default (instead of false)
this.key = key;
}


This leads to the following output:




  • true

  • Opened

  • Opened






share|improve this answer













If I understand it right, you want to the Locks to be OPEN by default? If so, just change to this:



Lock(int key) 
{
isOpen = true; // < true as default (instead of false)
this.key = key;
}


This leads to the following output:




  • true

  • Opened

  • Opened







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 3:04









Big LebowskiBig Lebowski

1214




1214













  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40



















  • Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

    – Joshua Gentile
    Nov 22 '18 at 3:40

















Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

– Joshua Gentile
Nov 22 '18 at 3:40





Pretty much what I need is that for each individual lock is that if the same lock receives 3 failed attempts on a row. It prints the message ALARM.

– Joshua Gentile
Nov 22 '18 at 3:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423199%2fjava-trying-to-adjust-the-attempts-a-user-has-to-unlock-a-lock-in-a-program-and%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]