Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Several remarks here:</p> <p>1) <code>thisLine.trim()</code> will just remove the trailing blanks at the beginning and ending of the <code>thisLine</code> 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.</p> <p>2) To split the two different values you should use <code>thisLine.split(" ")</code> (I'm assuming that your CSV file uses blanks to separate the different fields).</p> <p>3) Another mistake is to compare the strings using <code>==</code> rather than <code>equals</code>, which is the correct way of doing it.</p> <p>4) Since you are reading a new line in the <code>while</code> condition you don't need the internal <code>reader.readLine()</code></p> <p>5) Finally, never close the stream (or reader) inside the loop!! Do it on a <code>try/catch/finally</code> block.</p> <p>So, with those corrections your code will look like:</p> <pre><code> 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 &lt;-" + user.displayUserName() + " -&gt; 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 */ } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload