We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,414 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Search in a text file

Hi, I'm doing a study planner in java. I'm doing a simple login system. For every user who registers a text file is created holding all of the information inputted during registration. This is the code of a method which the program uses to login the user:

public void loginUser(){

    r = new Registration();

    r.username = txtUser.getText();
    r.password = txtPass.getText();

    try{


        String line;

        SubjectFrame subject = new SubjectFrame();

        BufferedReader f = new BufferedReader(new FileReader(r.username + ".txt"));


        while((item = f.readLine()) != null){

            if(line.equals(r.password)){

                dispose();
                subject = new SubjectFrame();
                subject.setSize(700,600);
                subject.setLocation(100,100);
                subject.setVisible(true);
            }else{
                System.out.println("No");
            }

        }
        f.close();
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "Wrong information!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

The problem is that when the user inputs wrong details I get 4 'No'('No' is used for debugging instead of an error message). When the user inputs the correct details I get 4 'No' but it proceeds properly. I know it's something concerning the loop but I can't figure it out. I'm still a beginner :(

Anyway thanks in advance!

3
Contributors
3
Replies
17 Hours
Discussion Span
8 Months Ago
Last Updated
4
Views
Question
Answered
Matth963
Light Poster
25 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Because the if/else/print"no" is inside the while loop it will be executed once for each line in the file. Your logic should check for the password being in the file, then display the frame or the error afterwards. Something like this (pseudo-code)

boolean passwordFound = false
read each line of the file...
   if password matches set passwordFound = true
after end of file...
   if (passwordFound) show frame
   else show error message
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Also the message: No is not too useful. Better for debugging would be to print out the values of line and r.password so you cann see what the computer is seeing so you'd know why the if test failed.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Thanks James !!

Matth963
Light Poster
25 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 8 Months Ago by JamesCherrill and NormR1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0629 seconds using 2.66MB