我正在构建一个Java GUI,要求用户输入其用户ID,如果我在文本文件列表中找到匹配项,则将其信息显示在GUI面板上。如果找不到他的ID,那么我会显示一个提示,要求他再次输入他的ID。

我现在使用的程序是,每次输入错误的ID后,即使我输入的下一个ID是正确的ID,我也无法在屏幕上显示正确的信息。我的GUI永远停留在该“错误ID”状态。我花了几个小时试图找出问题所在,但找不到。任何帮助,将不胜感激!



  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();
            }
      }
  }

最佳答案

我认为问题在于您不是在本地声明line。在方法开始时,尝试声明line

10-07 13:38
查看更多