Loop from a specific point to another Java point?

I am working on a Java project that looks like a digital time machine, basically saves what happened on a particular server, archives it into a specific file and saves it in a directory somewhere for safe storage. Now I'm working on a password function. I have the basics to check for correctness or incorrectness, but when I enter the wrong answer, the class ends after you say "Invalid password." Is there a way for it to loop from the end of the else to the start of main , so I don't need to re-execute the class every time?

 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"); } } } 
+5
source share
8 answers

You can use the while while loop if the user input is not valid and then continue the loop yet.

 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); } } 

Demo

+3
source

Whenever you want to run some code, check the result, and then repeat if necessary the most obvious do while structure.

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)); 
+2
source

Just put all the code in a while (true) loop that you break if the password is invalid

 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 } } } 
0
source
  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..."); } 
0
source

It is a good idea to have a limited number of password attempts. For example, for three attempts to enter a password, you will 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"); } } 
0
source

I added one while loop and one flag to your program. Please see:

 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"); } } } 
0
source

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); 
0
source

Use a do-while loop. In addition, your code has some security issues:

  • Do not use System.in for passwords. Use the console instead. (However, if you run the program from the IDE, you may not have a console. In this case, use the GUI method instead.)

  • Hash your password immediately after entering it.

  • Clear the password buffer as soon as possible after entering (i.e. rewrite 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); 
0
source

Source: https://habr.com/ru/post/1239026/


All Articles