【问题标题】:While loop exits with non-matching username / passwordWhile 循环以不匹配的用户名/密码退出
【发布时间】:2019-01-22 11:42:33
【问题描述】:

所以这只是我程序的一小部分,但实际上我要做的是进行基本的身份验证 while 循环。我不确定是否应该使用 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 条件下应该是|| 而不是&amp;&amp;。 (我希望那不是你的真实密码……)
  • 它不起作用,因为我确实有代码。但如果我输入了错误的凭据,它仍然会继续。
  • 提供minimal reproducible example,否则我们都不清楚是哪一部分不起作用。就像现在一样,您不完整的代码根本没有任何意义。

标签: java authentication while-loop do-while


【解决方案1】:

简单回答你的问题,

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

}

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

【讨论】:

  • 我不熟悉 PreparedStatement,但我会做一些研究。谢谢
  • 如果建议对您有用,您需要标记它(单击对勾)。这样,其他人就不会再看一遍问题,而是会继续帮助其他未回答的问题。
  • 应该是!existingPassWord.equals(...)
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 2021-04-10
  • 2015-08-15
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多