To count password and username access possibility

ehmaster

Mitglied
Hello java community,


i need again your advices:(. So i have actionPerformed for my button to check password and username validation. So i also want to count all (Right and wrong) possibility and print on the screen. For example y will try to acces 5 times and one is right. At the end you will show that 4 times was wrong credentials and 1 was right.

thanks!!!!


Java:
addBtn.addActionListener(new ActionListener() {


			@Override
			public void actionPerformed(ActionEvent arg0) {


				String name = nameField.getText();


				String password = passwordField.getText();


				String text = "";


				if (password.equals("2005") && name.equals("aaaa")) {


					text = "Your  Credentials  are  Right";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#058B2B><b>You have Succesfully Logged IN :)</Center></font>",
									"Information Message",
									JOptionPane.INFORMATION_MESSAGE);


				}


				if (password.length() > 3 && name.length() > 8
						&& !password.equals("2005")
						&& !name.equals("aaaa")) {


					text = "Your  Credentials  are  Wrong";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<font color=#FA0808><b> Casual Access</Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				if (password.length() < 3 && name.length() < 8
						&& !password.equals("2005")
						&& !name.equals("aaaa") && password.length() != 0
						&& name.length() != 0) {


					text = "Your  Credentials  are  Wrong";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b>  Casual Access </Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				if (password.length() == 0 && name.length() == 0) {


					text = "Please fill your credentials";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b> Access is denied </Center></font>",
									"Information Message",
									JOptionPane.ERROR_MESSAGE);


				}


				if (password.length() == 0 && name.length() > 0
						&& !name.equals("aaaa")) {


					text = "Password  = Null";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b>  Casual Access </Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				if (password.length() > 0 && name.length() == 0
						&& !password.equals("2005")) {


					text = "username is null and password is not right";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b>  Casual Access </Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				if (password.equals("2005") && name.length() >= 0
						&& password.length() != 0 && !name.equals("aaaa")) {


					text = "*******************************";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b>  Casual Access </Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				if (password.length() >= 0 && name.equals("aaaa")
						&& !password.equals("2005")) {


					text = "11111111111111111111111111";
					JOptionPane
							.showMessageDialog(
									new JFrame(),
									"<html><Center>"
											+ "<Center><font color=#FA0808><b>  Casual Access </Center></font>",
									"Information Message",
									JOptionPane.WARNING_MESSAGE);


				}


				text += " \n" + "Username:" + name + "\n" + "Password:"
						+ password + "\n" + "\n";


				fireDetailEvent(new DetailEvent(this, text));


			}


		});
 
Zuletzt bearbeitet:

Major_Sauce

Bekanntes Mitglied
I´m not sure where the problem is.
Just create a variable called wrongTries inside your class and in your actionPerformed method just increase that if the password was wrong.

regards Major
 

Major_Sauce

Bekanntes Mitglied
You don´t need a loop for counting that, because your "actionPerformed" method is your "loop", more or less.
You just need a variable outside of the actionPerformed void, which is then counted up...

Here´s an example:

Java:
int wrongTries = 0;
	
	public void actionPerformed(){
		//do something
		
		String password = "2004";
		
		if(!password.equals("2005")){
			//Increase because the password is wrong
			wrongTries++;
		} else {
			System.out.println("You needed " + wrongTries + " tries to log in...");
			//Resetting, you may not need that...
			wrongTries = 0;
		}
	}

regards Major
 

ehmaster

Mitglied
No, i have one question but i don't want to disturb you ((((. But if you have any idea just advice me how to change (text) color in textArea if i have error message . I want to get different occasion different color.

Thank you so much for your attention!!!!!!!!!!!!!
 

ehmaster

Mitglied
Yes it is right but when you have actionperformed in text Area and you want to change some parts of the text then it is not working. As i guessed this function has no in java swing.
 

Major_Sauce

Bekanntes Mitglied
I´m not sure if there is something like color codes...
You could just stop using TextFields and override the paintComponent of your JPanel instead. This will let you draw everything like you want.
 

Ähnliche Java Themen

Neue Themen


Oben