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 8656777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:21:48+00:00 2026-06-12T15:21:48+00:00

Starting a project, and this irriating me. I think the code is okay, but

  • 0

Starting a project, and this irriating me. I think the code is okay, but I don’t understand how to either return a record or parse the line.

I have a CSV file, which displays like this in Libra.

USER PASSWORD
Chris Password
Michelle Password
John Password

I am getting the user to enter their user and password, and then trying to compare those against the CSV to make sure that 1) the username exists and 2) if it does, that the password is correct. To be honest, part 2 isn’t essential as it’s not part of the spec but I want to do it, and I suspect it would be the same problem as what I am having anyway. As I said, I think the code is okay, but I’m not sure what format the variable thisLine will have and is my else statement moving the BufferedReader onto the next line correctly?

Can I use thisLine.trim() to cut USER PASSWORD does to just USER?

static void readFromCsvFile(String sFileName, User user) throws FileNotFoundException
{
String thisLine;

        try
        {
           BufferedReader reader = new BufferedReader(new FileReader(sFileName));
           thisLine = reader.readLine();
           System.out.print(thisLine);

           while((thisLine = reader.readLine()) != null)
               {
                    if (user.displayUserName() == thisLine)
                    {
                    System.out.print("\nUser <-" + user.displayUserName() + " -> exists!");
                    reader.close();
                    }

                    else 
                    {
                        thisLine = reader.readLine();
                    }
               }

        }
        catch(IOException e)
        {
            System.out.print("\nUser does not exist\n"); 
            e.printStackTrace();
        } 
  • 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-12T15:21:49+00:00Added an answer on June 12, 2026 at 3:21 pm

    Several remarks here:

    1) thisLine.trim() will just remove the trailing blanks at the beginning and ending of the thisLine contents. It is okay to do so, specially if you are going to compare two strings, but it won’t split the username and password from the variable.

    2) To split the two different values you should use thisLine.split(" ") (I’m assuming that your CSV file uses blanks to separate the different fields).

    3) Another mistake is to compare the strings using == rather than equals, which is the correct way of doing it.

    4) Since you are reading a new line in the while condition you don’t need the internal reader.readLine()

    5) Finally, never close the stream (or reader) inside the loop!! Do it on a try/catch/finally block.

    So, with those corrections your code will look like:

     static void readFromCsvFile(String sFileName, User user) throws FileNotFoundException { 
       String thisLine;
       BufferedReader reader = new BufferedReader(new FileReader(sFileName));
        try
        {
    
           thisLine = reader.readLine();
           System.out.print(thisLine);
    
           while((thisLine = reader.readLine()) != null)               
               {
                   thisLine = thisLine.trim();
                   String username = thisLine.split(" ")[0];
                    if (user.displayUserName().equals(username))
                    {
                    System.out.print("\nUser <-" + user.displayUserName() + " -> exists!");
                    break;   // break the loop
                    }                    
               }
    
        }
        catch(IOException e)
        {
            System.out.print("\nUser does not exist\n"); 
            e.printStackTrace();
        } 
        finally {
            try {
                reader.close();
            } catch (IOException e) { /* ignore */ }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a .Net guy, and I'm interested in starting this project but I'm not
I'm starting to creat this project using MVVM Model. But I have no idea
I've tried Infragistics on this but they don't seem to understand what I am
I'm starting a new project, for this project, I need a great IDE, which
I'm starting on a new iPhone project, and this application mostly relies on MySQL.
I'm starting a new project, and this must be done in Delphi, so we
Maybe I am not thinking about this correctly. I am starting my second project
I'm starting this new project and we are thrashing out our logging/debugging approach and
Apologies for the newbishness of this question. I'm just starting a project and I
Has anyone done something like this? How? I'm just starting a project that will

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.