Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java While循环以不匹配的用户名/密码退出_Java_Authentication_While Loop_Do While - Fatal编程技术网

Java While循环以不匹配的用户名/密码退出

Java While循环以不匹配的用户名/密码退出,java,authentication,while-loop,do-while,Java,Authentication,While Loop,Do While,所以这只是我程序的一小部分,但本质上我要做的是在循环时进行基本的身份验证。我不确定是否应该使用while循环或其他方法。 如果用户名和密码与帐户匹配,我希望程序停止循环。 我不确定这个问题是否清楚,但这是我能做的最好的了。如果您需要任何澄清,请告诉我 编辑:如果我输入了错误的用户名或密码,它不会再次循环。那是我的问题 这是我遇到问题的地方: User user2 = new User(); userList.add(user2); String userLogin

所以这只是我程序的一小部分,但本质上我要做的是在循环时进行基本的身份验证。我不确定是否应该使用while循环或其他方法。 如果用户名和密码与帐户匹配,我希望程序停止循环。 我不确定这个问题是否清楚,但这是我能做的最好的了。如果您需要任何澄清,请告诉我

编辑:如果我输入了错误的用户名或密码,它不会再次循环。那是我的问题

这是我遇到问题的地方:

User user2 = new User();
        userList.add(user2);
        String userLogin = "estebangong23";
        String userPass = "twitterisfun";
        String existingUserName = " ";
        String existingPassWord = " ";

        user2.setUserName("estebangong23");
        user2.setEmailAddress("esteban.gong@yahoo.com");
        user2.setPassWord("twitterisfun");

        System.out.println("Please sign in here.");

        while (!existingUserName.equals("estebangong23") && existingPassWord.equals("twitterisfun")) {

            System.out.println("Username: ");
            existingUserName = sc.nextLine().toLowerCase();

            System.out.println("Enter your password: ");
            existingPassWord = sc.nextLine().toLowerCase();

            //System.out.println("Log In unsuccessful. Incorrect username or password.");
        } 
以下是我的大部分计划:

    import java.util.*;
    import java.io.*;

    public class MyClass {
    public static void main(String args[]) {
    // make it so u can log in to an existing account
    // make it double check the password and email

    Scanner sc = new Scanner(System.in);

    List<User> userList = new ArrayList<User>();
    List<String> matchesList = new ArrayList<String>();
    String date = "03/21/2019";
    String tweet = " ";

    System.out.println("-------------------------------------------------------------------------");
    System.out.println("|                               Twitter                                 |");
    System.out.println("| Sign Up                                                        Log in |");
    System.out.println("-------------------------------------------------------------------------");

    System.out.println("Would you like to Log In or Sign Up?");
    String responseString = sc.nextLine().toLowerCase();


    if (responseString.equals("signup") ) {

        User user1 = new User(); //this creates the first user

        System.out.println("Alright, lets get started by setting up your profile!");

        System.out.println("Please enter a username.");
        user1.setUserName(sc.nextLine());

        System.out.println("Please enter your email address.");
        user1.setEmailAddress(sc.nextLine());

        System.out.println("Please enter a password.");
        user1.setPassWord(sc.nextLine());

        System.out.println("Alright we got your account all setup. Just take a moment to review everthing. Don't worry you can change this stuff later if you want!");
        user1.printUserProfile();

        userList.add(user1);

        System.out.println("Your account is all setup, go ahead and try posting something for the first time!");


        } else { //end signup if

        User user2 = new User();
        userList.add(user2);
        String userLogin = "estebangong23";
        String userPass = "twitterisfun";
        String existingUserName = " ";
        String existingPassWord = " ";

        user2.setUserName("estebangong23");
        user2.setEmailAddress("esteban.gong@yahoo.com");
        user2.setPassWord("twitterisfun");

        System.out.println("Please sign in here.");

        while (!existingUserName.equals("estebangong23") && existingPassWord.equals("twitterisfun")) {

            System.out.println("Username: ");
            existingUserName = sc.nextLine().toLowerCase();

            System.out.println("Enter your password: ");
            existingPassWord = sc.nextLine().toLowerCase();

            //System.out.println("Log In unsuccessful. Incorrect username or password.");
        } 

        } //end else

    //main program
    System.out.println("-------------------------------------------------------------------------");
    System.out.println("Let's post something! Show the world what your thinking about righ now!");
    System.out.println("If you want to stop entering tweets at any time, please enter '**'. ");

    System.out.println(" ");

    while (!tweet.equals("**")) {

        System.out.println("Enter your tweet here: ");
        tweet = sc.nextLine().toUpperCase();

        String[] tweetArray = tweet.split(" ");

        System.out.println("");




    } //end tweet while loop


    System.out.println("-------------------------------------------------------------------------");

    } //end main

    } //end main class

    class User {
    // this class sets up the users profile when they are signing up
private String userName;
private String emailAddress;
private String passWord;
public List<Tweet> tweetList = new ArrayList<Tweet>();

//Username, Age, Email Address, Tweets[ ]
//Methods
//Setters and getters, Create tweet
public User () {

}

public String getUserName() {return this.userName;}
public void setUserName(String userName) {this.userName = userName;}

public String getEmailAddress() {return this.emailAddress;}
public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}

public String getPassWord() {return this.passWord;}
public void setPassWord(String passWord) {this.passWord = passWord;}

public void printUserProfile() {
    System.out.println("Username: " + this.userName);
    System.out.println("Email Address: " + this.emailAddress);
    System.out.println("Password (remember not to share this to anyone) " + this.passWord);
}

}简单地回答你的问题

while (! (existingUserName.equals(userLogin) && existingPassWord.equals(userPass)) ) { // if users doesn't exist
...

}

但是,您需要重新考虑用于身份验证的代码。我猜你才刚刚开始。首先,将它们放在一个表中,然后使用PreparedStatement访问它们。

好吧,它是否有效?我没有看到问题陈述。看起来代码正在工作,但您没有循环后的成功代码场景。在while条件下,这应该是| |而不是&&。我希望那不是你真正的密码…它不起作用,因为我有密码。但是如果我输入了错误的凭证,它仍然会继续。请提供一个,否则我们都不知道哪一部分不起作用。就目前而言,您不完整的代码根本没有任何意义。我不熟悉PreparedStatement,但我会做一些研究。谢谢如果建议对你有效,你需要标记它,点击勾号。这样,其他人就不会再仔细检查这个问题,而是继续帮助其他未回答的问题。应该这样!现有密码。等于。。。。