Password 5 attempts Learn programming Java

Lesson:

Basic Data Types


Exercise:

Password 5 attempts 43


Objetive:

Write a java program which asks the user for his login and password. Both must be strings. After 5 wrong attempts, the user will be rejected.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		String user, pass;
		int count = 0;

		do
		{
			System.out.print("Enter username: ");
			user = new Scanner(System.in).nextLine();

			System.out.print("Enter password: ");
			pass = new Scanner(System.in).nextLine();

			count++;

		} while (((!user.equals("user")) || (!pass.equals("password"))) && (count != 5));

		if (count == 5)
		{
			System.out.print("Login attemp fail!");
		}
		else
		{
			System.out.print("Password correct!");
		}
	}
}