Password 5 attempts Learn programming Java



Lesson:

Basic Data Types


Exercise:

Password 5 attempts


Objetive:

Write a java program that prompts the user for their username and password. Both should be strings. After 5 incorrect 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!");
		}
	}
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.