如何在Java中创建注册和登录ID

如何在Java中创建注册和登录ID,java,Java,我想问一下我的编程代码。似乎有个错误。请检查我下面的代码,它没有达到我的预期。问题是,我输入了正确的用户名,但它再次重复。我非常感谢你的回答 static Scanner scan; static ArrayList<String> EmailList; static ArrayList<String> FullNameList; static ArrayList<String> UsernameList; static ArrayList<String

我想问一下我的编程代码。似乎有个错误。请检查我下面的代码,它没有达到我的预期。问题是,我输入了正确的用户名,但它再次重复。我非常感谢你的回答

static Scanner scan;
static ArrayList<String> EmailList;
static ArrayList<String> FullNameList;
static ArrayList<String> UsernameList;
static ArrayList<String> PasswordList;

public static void clear(){
    for(int i =0; i < 24; i++){
        System.out.println();
    }
}

public static boolean ouragreement (String agreement){
    if(agreement.isEmpty()){
        return false;
        }
    else if(agreement.equalsIgnoreCase("N") || agreement.equalsIgnoreCase ("No")){

        return false;

        }
    return true;

    }

public static boolean validateusername (String username, ArrayList<String>UsernameList){
    if(username.isEmpty()){

        System.out.println("Username cannot be empty");
        return false;

        }

    else if(!username.equals(UsernameList.size())){

        System.out.println("Please input again your username");
        return false;

        }
    return true;

    }//validateusername


public static boolean validatepassword(String password, ArrayList<String>PasswordList){

    if(password.isEmpty()){

        System.out.println("Password cannot be empty");
        return false;
        }
    else if(!password.equals(PasswordList.size())){

        System.out.println("Please input again your password");
        return false;

        }

    return true;



    }//validatepassword




public static void case1(){

    String email, fullname, username, password, agreement;

    System.out.print("Input your email : ");
    email = scan.nextLine();
    System.out.print("Input your Full name : ");
    fullname = scan.nextLine();
    System.out.print("Input your username : ");
    username = scan.nextLine();
    System.out.print("Input your password : ");
    password = scan.nextLine();

    EmailList.add(email);
    FullNameList.add(fullname);
    UsernameList.add(username);
    PasswordList.add(password);


    do{
        System.out.print("If you say Yes[Y], you agreed to our Terms & Privacy Policy [Y/N] : ");
        agreement = scan.nextLine();
    }while(!ouragreement(agreement));


    clear();
        System.out.println("Sign Up Success!");

        System.out.println();



    }//case1




public static void case2(){

    String username,password;

    do{
        System.out.print("Username : ");
        username = scan.nextLine();
    }while(!validateusername(username,UsernameList));

    do{
        System.out.print("Password : ");
        password = scan.nextLine();
    }while(!validatepassword(password,PasswordList));


    System.out.println("Welcome");







    }//case2



public static void inisialisasi (){

    scan = new Scanner (System.in);
    EmailList = new ArrayList<>();
    FullNameList = new ArrayList <>();
    UsernameList = new ArrayList<>();
    PasswordList = new ArrayList<>();


    }//inisialisasi




public static void main (String [] args){

    inisialisasi();

    int choose;

    do{
    System.out.println("\tKOSTFINDER APPLICATION");
    System.out.println("\t======================\n");


    System.out.println("Welcome to KostFinder Application, please choose option below");

    System.out.println("1. Sign up");
    System.out.println("2. Login");
    System.out.print("Choose :");
    choose = scan.nextInt(); scan.nextLine();
    switch(choose){
        case 1 :
        case1();
        break;

        case 2 :
        case2();
        break;


        }
}while(choose != 101);






    }//main






}//public class

问题在于这两种方法:

public static boolean validateusername (String username, ArrayList<String>UsernameList){
    if(username.isEmpty()){

        System.out.println("Username cannot be empty");
        return false;

        }

    else if(!username.equals(UsernameList.size())){

        System.out.println("Please input again your username");
        return false;

        }
    return true;

}//validateusername


public static boolean validatepassword(String password, ArrayList<String>PasswordList){

    if(password.isEmpty()){

        System.out.println("Password cannot be empty");
        return false;
        }
    else if(!password.equals(PasswordList.size())){

        System.out.println("Please input again your password");
        return false;

        }

    return true;



}//validatepassword

注意:这种存储密码的方式非常不安全。我知道这不是生产代码,但在生产代码中从来没有这样做过。至少你应该做一个散列算法。

条件如果!username.equalsUsernameList.size将始终为true,因为您正在将字符串与整数进行比较。如果您希望检查列表中是否包含您的用户名,请显示引发的错误。请参阅:。在发布之前,您甚至都没有费心完成2分钟的站点浏览。谢谢您的回答Buda先生,对不起,我正在将数据类型字符串与.equals进行比较。如果是int,我认为int应该与==或!=进行比较。所以我觉得没问题。谢谢你的反馈,我会记住@T-Heron谢谢你的回答Sweeper先生,当我把你的代码放在我的textpad 7中时,出现了一个错误,消息是错误:不兼容类型String correctPassword=PasswordList.indexOfindex;^必需:找到字符串:int 1错误如何修复?感谢You@Hendra哎呀!我的错!那是个打字错误。将该行替换为字符串correctPassword=PasswordList.getindex;非常感谢你,清道夫先生,你的回答真的帮助了我:“亨德拉,如果你认为我的答案回答你的问题,请考虑通过点击复选来接受它!
public static boolean validateUsernameAndPassword(String username, String password) {
    int index = UsernameList.indexOf(username);
    if (index == -1) { 
        System.out.println("The user name does not exist");
        return false; 
    }
    String correctPassword = PasswordList.get(index);
    if (correctPassword.equals(password)) {
        return true;
    } else {
        System.out.println("Please input again your password");
        return false;
    }
}