i want a program which asks the user to type the password. If he goes wrong three times exit.this is my code
import java.io.*;
class password1
{
public static void main(String args[])throws Exception
{
String pass="samarth";
boolean flag=false;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String [] password=new String[20];
int i;
for(i=0;i<3;i++)
{
System.out.println("enter password");
password[i]=br.readLine();//*
if(pass.compareTo(password[i])==0)
{
flag=true;
break;
}
else
{
System.out.println("try again");
password[i]=br.readLine();
}
}
if(flag)
System.out.println("welcome");
}
}
the line marked with * is the problem because it asks for password again
enter password
nikhil
try again
nikhi//i have to put this because of the line marked *
enter password
nikh
try again
nikh
enter password
samarth
welcome
i am sorry in advance if i am not clear
akulkarni 0 Junior Poster
zeeven 0 Newbie Poster
zeeven 0 Newbie Poster
akulkarni 0 Junior Poster
zeeven 0 Newbie Poster
tux4life 2,072 Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.