user2350622
user2350622

Reputation: 77

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. If his id is not found, then I display a prompt to ask him to type in his id again.

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. My GUI stays at that "wrong id" sate forever. I spent hours trying to figure out what went wrong but I just could not find it. Any help would be appreciated!

The GUI screenshot

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

Upvotes: 0

Views: 1901

Answers (1)

APerson
APerson

Reputation: 712

I think the problem is that you are not declaring line locally. In the beginning of the method try declaring line.

Upvotes: 1

Related Questions