开发者

Java password Program loops forever need help

开发者 https://www.devze.com 2023-02-08 15:41 出处:网络
Why is this Just repeating forever even when I enter a length more than 6 characters? import java.util.Scanner;

Why is this Just repeating forever even when I enter a length more than 6 characters?

import java.util.Scanner;

class Password {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome please enter your username and password.");
        System.out.print("Username >>");
        input.nextLine();
        enterPassword();
        System.out.println("Successfully Logged In");
        }

    public static void enterPassword(){
        String password;
        Scanner input = new Scanner(System.in);
        System.out.print("Password >>");
        password = input.nextLine();
        checkPasswordLength(password);
        }


    public static void checkPasswordLength(String password){
        int length;
        length = password.length();
        while (length <6){
            enterPassword();
            }
        checkPasswordLetter(p开发者_JAVA百科assword);
        }

    public static void checkPasswordLetter(String password){
        System.out.println("More checking here to be added");
        }
}


    length = password.length();
    while (length < 6){
        enterPassword();
    }

You never update length, even after obtaining a new password.

Here's a better way to organize your code:

public static String enterPassword() {
   //gets a string and returns it
}

public static boolean checkPasswordLength(String password) {
   //if too long return false
}

//...
String password = enterPassword();
while ( !checkPasswordLength(password) ) {
    password = enterPassword();
}


You have a few issues. The first is obviously that length doesnt change in your while loop, secondly enterPassword() doesnt actually change the password.

Also you are infinitely calling enterpassword that calls checklength that calls enterpassword that calls..... not sure if this is the best habit to get into.

You should try and break your functions into logical units of work so that they are more reusable.

enterpassword should enter the password, then checkpasswordlength should check the length independantly, IMO

how about something liek this?

 import java.util.Scanner;

class Password {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Welcome please enter your username and password.");
    System.out.print("Username >>");
    input.nextLine();

    String password = "";

    //keep going until we get an acceptable password
    while(!CheckPassword(password))
    {     
    password = enterPassword();
    }
    System.out.println("Successfully Logged In");
    }


    public static Boolean CheckPassword(String password)
    {
    //perform all password checks
    Boolean passedLength = checkPasswordLength(password);
    Boolean passedLetter = checkPasswordLetter(password);
    return (passedLength && passedLetter);                 

    }

public static String enterPassword(){
    String password;
    Scanner input = new Scanner(System.in);
    System.out.print("Password >>");
    password = input.nextLine();    


    return password;
    }


public static Boolean checkPasswordLength(String password){
    //passes if there is a string value, and it has 6+ characters
    return (password != null && password.length >=6);        
    }

public static Boolean checkPasswordLetter(String password){
    System.out.println("More checking here to be added");
    return true; //for now....
    }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消