I'm working on a project in Java that is kind of like a digital time-machine, it basically saves what happened on a specific sate, archives it to a specific file, and stores it in a directory somewhere for safe keeping. Right now I'm working on a password function. I have the basics down for checking if it's right or wrong, but when I input the wrong answer, the class ends after saying "Invalid Password". Is there a way to have it loop from the end of else
statement to the beginning of main
so I don't have to re-execute the class everytime?
import java.util.Scanner;
import java.util.*;
public class Adam {
public static void main(String args[]) {
String passwrd = "password";
String inputPassword;
System.out.print("Please Input Password ");
Scanner sc = new Scanner(System.in);
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
} else {
System.out.println("Invalid Password");
}
}
}
You can use a do while loop, if user input is invalid then continue the loop else not.
import java.util.Scanner;
import java.util.*;
public class Adam {
public static void main(String args[]) {
String passwrd = "password";
String inputPassword;
boolean b = true;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Please Input Password ");
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
b = false;
} else {
System.out.println("Invalid Password");
b = true;
}
} while (b);
}
}
Whenever you want to run some code, check the result and then repeat if necessary the most obvious structure is do while
.
Something like the following:
String inputPassword = null;
do {
if (inputPassword != null)
System.out.println("Invalid password");
System.out.println("Enter password");
inputPassword = sc.nextLine();
} while (!inputPassword.equals(password));
Just put whole code into while(true) loop which you will break if password is walid
public static void main(String args[])
{
String passwrd = "password";
String inputPassword;
while(true)
{
System.out.print("Please Input Password ");
Scanner sc = new Scanner(System.in);
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd))
{
System.out.println("Password Accepted, Loading Archive...");
break; //here you are getting out of loop
}
else
{
System.out.println("Invalid Password");
//you are not breaking loop so the program will return to the beggining
}
}
}
public static void main(String args[]) {
String password = "password";
String inputPassword;
while(inputPassword == null || !inputPassword.equals(password)){
System.out.print("Please Input Password ");
Scanner sc = new Scanner(System.in);
inputPassword = sc.nextLine();
if (!inputPassword.equals(password)) {
System.out.println("Invalid Password");
}
}
System.out.println("Password Accepted, Loading Archive...");
}
It's a good idea to have a limited number of tries to input the password. For example to have three tries to enter password, you'd have this code:
Scanner sc = new Scanner(System.in);
for (i = 0; i < 4; ++i) {
System.out.print("Please Input Password ");
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
break;
}
else {
System.out.println("Invalid Password");
}
}
I have added one while loop and one flag in your program. Please have a look:
public static void main(String[] args) {
String passwrd = "password";
boolean wrongPassword = true;
while(wrongPassword){
String inputPassword;
System.out.print("Please Input Password ");
Scanner sc = new Scanner(System.in);
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
wrongPassword = false;
}
else {
System.out.println("Invalid Password");
}
}
}
use a do-while loop :
String passwrd = "password";
String inputPassword ="";
Scanner sc = new Scanner(System.in);
do{
System.out.print("Please Input Password ");
// get the password from user
inputPassword = sc.nextLine();
// if password matches print success and break out of loop
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
break;
}
else{
System.out.println("Invalid Password");
}
}while(true);
Use a do-while loop. Also, your code has some security issues:
Don't use System.in for passwords. Use Console instead. (However, if you run the program from an IDE, you may not have a Console. In that case, use a GUI method instead.)
Hash your password straight after you input it.
Clear your password buffer as soon as possible after input (ie overwrite it). See Console documentation.
Console con = System.console(); if (con == null) throw new IOException("This JVM has no console"); boolean pwdCorrect; do { char[] inputPassword = con.readPassword("Please input password: "); String hashedInputPassword = hashPassword(inputPassword); // search Internet for how to hash a password for (int i = 0; i < inputPassword.length; i++) inputPassword[i] = '\\0'; pwdCorrect = isValidPassword(hashedInputPassword); // eg by looking it up in a password file if (pwdCorrect) System.out.println("Password accepted. Loading archive..."); else System.out.println("Invalid password."); } while (!pwdCorrect);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.