I'm a student,
I have been working on a project that heavily features OOP. I have run into a problem that doesn't make sense logically. I have created an instance for a password class and then used its name to call a method within the class. The instantiated class code is displayed below:
import javax.swing.JOptionPane;
public class password {
private String password;
private int attLimit;
private String passwordAttempt;
private boolean login;
public password(){
password = "admin";
attLimit = 0;
passwordAttempt = " ";
login = false;
}
public void passwordAttempt() {
attLimit = 0;
login = false;
for(int i = 0; i>5; i ) {
attLimit = attLimit - i;
passwordAttempt = JOptionPane.showInputDialog("Please input password");
if(passwordAttempt == password) {
JOptionPane.showMessageDialog(null, "Log in succesfull");
login = true;
i = 5;
}
else {
JOptionPane.showMessageDialog(null, attLimit " attempts left");
}
}
}
}
instantiating and calling method name:
import javax.swing.JOptionPane;
public class QuizApp {
public static void admin() {
password ps = new password();
ps.passwordAttempt();
if(ps.isLogin() == false) {
JOptionPane.showMessageDialog(null, "Log in failed");
welcome();
}
else if(ps.isLogin() == true) {
JOptionPane.showMessageDialog(null, "Log in successful");
}
}
When ps.passwordAttempt is called the whole app just crashes and stops working. I ran the problem in debug and the second the IDE steps into the command it just cuts short with no error messages. Is this an IDE issue or is it my code? is the code too inefficient maybe?
CodePudding user response:
Instead of jumping to crazy conclusions ("If my code is too inefficient, maybe java decides to randomly just do nothing instead?"), debug your code.
for(int i = 0; i>5; i ) {
That would never fire. i = 0
, which is not larger than 5, so the loop doesn't even run. I think you meant for (int i = 0; i < 5; i )
instead.