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
Recommended Answers
Jump to PostString [] password=new String[20];
what's the use of the array of string? you are saving all the passwords inputed, why?
try this
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; int …
Jump to Postthe proble u encountered is because of ur second readLine statement in the else statement:
if(pass.compareTo(password)==0)
{
flag=true;
break;
}
else
{
System.out.println("try again");
password=br.readLine();
}
All 5 Replies
zeeven
0
Newbie Poster
zeeven
0
Newbie Poster
akulkarni
0
Junior Poster
zeeven
0
Newbie Poster
mvmalderen
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.