简体   繁体   English

Java GUI:while循环和if / else语句

[英]java GUI: while loop and if/else statement

I am building a java GUI which asks the user to type in his user id and if I find a match in my text file list, then I display his info to the GUI panel. 我正在构建一个Java GUI,要求用户输入其用户ID,如果我在文本文件列表中找到匹配项,则将其信息显示在GUI面板上。 If his id is not found, then I display a prompt to ask him to type in his id again. 如果找不到他的ID,那么我会显示一个提示,要求他再次输入他的ID。

The program I am having right now is that every time after I type in a wrong id, even if the next id I type in is a correct one, I could not display the right info to the screen. 我现在使用的程序是,每次输入错误的ID后,即使我输入的下一个ID是正确的ID,我也无法在屏幕上显示正确的信息。 My GUI stays at that "wrong id" sate forever. 我的GUI永远停留在该“错误ID”状态。 I spent hours trying to figure out what went wrong but I just could not find it. 我花了几个小时试图找出问题所在,但找不到。 Any help would be appreciated! 任何帮助,将不胜感激!

GUI屏幕截图

  private class okButtonListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
          FileReader fr = null;
            try{
                fr = new FileReader("charList.txt");
            }
            catch (FileNotFoundException e1){
                e1.printStackTrace();
            }
            BufferedReader bf = new BufferedReader(fr);

            userID = Integer.parseInt(idField.getText());

            while(line != null){
                try{
                    line = bf.readLine();
                }
                catch (IOException e1){
                    e1.printStackTrace();
                }
                if (line != null){
                    fields = line.split("\t");
                    if (userID == Integer.parseInt(fields[0])){
                        System.out.println(fields[2]);
                        displayFieldsSelections();
                        resetFields();
                        break;
                    }
                }
            }

            if (userID != Integer.parseInt(fields[0])){
                mistakeResetFields();   
            }
      }
  }

I think the problem is that you are not declaring line locally. 我认为问题在于您不是在本地声明line In the beginning of the method try declaring line . 在方法开始时,尝试声明line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM