Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:39:12+00:00 2026-06-17T21:39:12+00:00

I wrote the following code to read two different text files. One text file

  • 0

I wrote the following code to read two different text files. One text file is called the “username” text file, while the other is called the “password” text file. I am creating a login program. In order for the user to login successfully, the line number of the username must match that of the password. For some reason however, my LineNumberReader for both files is stuck at zero, which is strange because I put assertions to avoid this and the assertions did not throw an error. I know they are zero because I printed out each reader. It also keeps printing out “Match found” even when I deliberately put in a wrong username-password match. Can anyone figure this out?

    public boolean usernamePasswordCheck(String username, String password) throws         FileNotFoundException, IOException{
    BufferedReader br = new BufferedReader(new FileReader("src/BioStat/username.txt"));
    BufferedReader br2 = new BufferedReader(new FileReader("src/BioStat/password.txt"));
    String line;
    String line2;
    int userLine=0;
    int passwordLine=0;
    LineNumberReader lnr = new LineNumberReader(new FileReader("src/BioStat/username.txt"));
    LineNumberReader lnr2 = new LineNumberReader(new FileReader("src/BioStat/password.txt"));
    while((line=br.readLine())!=null){
        if(line.equals(username)){
            //username is found in username list. There is a match!
            //let's get it's line number
            userLine = lnr.getLineNumber();
        }else{
            //username was not found in list, so we should keep reading.
            line = br.readLine();
        }
    }
    System.out.println("Userline: "+userLine);
    br.close();
    while((line2 = br2.readLine())!=null){
        if(line2.equals(password)){
            //username is found in username list. There is a match!
            //let's get it's line number
            passwordLine = lnr2.getLineNumber();

        }else{
            //username was not found in list, so we should keep reading.
            line2 = br2.readLine();
        }
    }
    System.out.println("passwordLine: "+passwordLine);
    br2.close();
    assert(userLine!=0);
    assert(passwordLine!=0);
    if(userLine==passwordLine){
        System.out.println("Match Found");
        matchfound = true;
    }else{
        System.out.println("Username and Password don't match");
    }
    return matchfound;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:39:13+00:00Added an answer on June 17, 2026 at 9:39 pm

    You aren’t advancing your LineNumberReader every loop. The while loop is taking care of advancing your BufferedReader.

    You can actually simplify things and just use the LineNumberReader without the BufferedReaders because their readLine will give you the text just as a BufferedReader will. For example:

    String line = null;
    int userLine=0;
    LineNumberReader lnr = new LineNumberReader(new FileReader("src/BioStat/username.txt"));
    while((line=lnr.readLine())!=null){
        if(line.equals(username)){
            //username is found in username list. There is a match!
            //let's get it's line number
            userLine = lnr.getLineNumber();
            break;
        }
    }
    lnr.close();
    

    The call to readLine on the LineNumberReader advances to the next line (and thus will increase the line number as well).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following piece of code to read a text file inside of
I want to compare two files so I wrote the following code: while($line1 =
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following code to select text from database,but when i echo the
I wrote the following code to count the node of an XML file: private
I have a piece of code to read two files, convert them to sets,
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
if wrote the following code and do not understand why the trace returns false:
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.