0

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();   
            }
      }
  }
5
  • what does mistakeResetFields method do? Commented Nov 9, 2013 at 17:07
  • First try to trace thru the code or put some System.err.println() to see where code may stuck. Commented Nov 9, 2013 at 17:10
  • Why line is not declared locally ? Commented Nov 9, 2013 at 17:12
  • try trim and then check if (userID == Integer.parseInt(fields[0].trim())){ Commented Nov 9, 2013 at 17:17
  • @PeterMmm Thank you so much! I declared line locally and it's working fine now! Commented Nov 9, 2013 at 17:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

If you could explains why this is causing the OP an issue, I think it will go along way to helping them understand the problem, as I spent half an hour trying to figure out the problem and how your answer related to it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.