Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
for循环后的代码不执行(Java)_Java_Loops_For Loop_Login_Dialog - Fatal编程技术网

for循环后的代码不执行(Java)

for循环后的代码不执行(Java),java,loops,for-loop,login,dialog,Java,Loops,For Loop,Login,Dialog,我有一小段代码,用来检查输入的代码名和密码与存储在文本文件中的内容。如果有比赛,比赛就开始了,一切都很好。但如果没有匹配项,则会弹出一个对话框,询问用户是否希望再次登录 int input=0; //yes do { codename=JOptionPane.showInputDialog(null,"Enter Codename: "); String password=JOptionPane.showInputDialog(null, "Enter Password: ")

我有一小段代码,用来检查输入的代码名和密码与存储在文本文件中的内容。如果有比赛,比赛就开始了,一切都很好。但如果没有匹配项,则会弹出一个对话框,询问用户是否希望再次登录

int input=0; //yes

do {
    codename=JOptionPane.showInputDialog(null,"Enter Codename: ");
    String password=JOptionPane.showInputDialog(null, "Enter Password: ");

    for(int i=0;i<users.length;i++){
        if((codename.equals(users[i].getCodeName())) && (password.equals(users[i].getPassword()))){
            System.out.println("\n\nCorrect");
            new Game();
        } else {
            System.out.println("\n\nIncorrect");
        }   
    }
    input = JOptionPane.showConfirmDialog(null, "Incorrect User name/Password\nWould you like to try again?");          
} while(input==0); //run while input is yes
我得到它,即使密码和代码名匹配,程序运行完美

上述代码的完整代码:

    public static void logOn(){
        //ASK FOR CODENAME & PASSWORD FROM TEXTFILE BEFORE GAME BEGINS
        //read from text file

        UserData[]users=new UserData[20];
        int countU=0;

        try{
            BufferedReader readU = new BufferedReader(new FileReader("userdata.txt"));

            String line;

            while((line=readU.readLine())!=null){
                String []parts=line.split("#");

                String codeName = parts[0];
                String password=parts[1];

                //              System.out.println(parts[0]);
                //              System.out.println(parts[1]);
                users[countU]=new UserData(codeName, password);
                countU++;
            }
            readU.close();
        }
        catch(FileNotFoundException e){

        }
        catch(IOException e){

        }

        //PASSWORD & CODENAME

        int input=0; //yes

        do{
            codename=JOptionPane.showInputDialog(null,"Enter Codename: ");
            String password=JOptionPane.showInputDialog(null, "Enter Password: ");

            for(int i=0;i<users.length;i++){
                if((codename.equals(users[i].getCodeName()))&&(password.equals(users[i].getPassword()))){
                    System.out.println("\n\nCorrect");
                    new Game();
                }
                else{
                    System.out.println("\n\nIncorrect");
                }   
            }

            input = JOptionPane.showConfirmDialog(null, "Incorrect Username/Password\nWould you like to try again?");           
        }
        while(input==0); //run while input is yes
    }
}

不要对(int i=0;i执行
for,而不是使用长度为20的数组,您可以使用ArrayList,因为它将动态展开:

List<UserData> users = new ArrayList<>();
并用

for(int i=0 ;i<users.size(); i++) {

for(int i=0;i用户数组看起来像什么?听起来像是抛出了一个异常,因为索引2之前的用户不包含您认为它包含的内容。控制台在完成运行后会说什么?@MasterYoda当密码/代码名错误时会这样说:com.ramtin.Game.logOn上的java.lang.NullPointerException(Game.java:505)位于com.ramtin.Game.main(Game.java:397)当它正确的时候,它也会说同样的话,但是它启动了游戏,一切都很好。用户数组由一个类图组成,其中包含一个字符串代码名和一个字符串密码变量。屏幕上的行是397还是505?这不是完整的代码,请发布用户数组及其内容的示例。是吗说
String name=users[i].getCodeName()
不起作用,但
String name=users[2].getCodeName()
是吗?非常感谢!这就解决了问题。我不敢相信我犯了这么愚蠢的错误。但现在我的问题是,在开始的游戏中,它仍然显示JOptionPane说密码错误。我只在输入=0的情况下运行while循环。所以不确定它为什么会这样做。对此有什么解决方案吗?
输入
始终是错误的o直到显示“坏密码”选项对话框后更改(如果更改)。编辑解决了弹出问题。只需中断do while循环。再次感谢您的帮助。谢谢,@geneconomiss的解决方案对我有效。但我将使用列表来防止错误。很高兴我能提供帮助。
List<UserData> users = new ArrayList<>();
users.add(new UserData(codeName, password));
for(int i=0 ;i<users.size(); i++) {