title: Reading CSV File Using Scanner Class Error
body: <p>I have an original code that came from a Java book example. The program simply reads a text file ("clients.txt").  However, I would like to modify the "clients.txt" into a "clients.csv" and have the program read ".csv" file instead.  I have parsed the data with "," as the delimiter according to this format and added/updated two lines of code in the <code>ReadTextFile.java</code> file below only:</p>
  
  <p>Added/updated Code: </p>
  
  <pre><code>input = new Scanner( new File( "clients.csv" ) );
  input.useDelimiter(",");    
  </code></pre>
  
  <p>Thought that this is a simple modification from reading a <code>.txt</code> file to a <code>.csv</code> file, I got a <code>NoSuchElementException</code> or <code>File improperly formed</code> print message.</p>
  
  <p>I know that there are other classes that I can use such as <code>BufferedReader</code> and/or <code>CsvReader</code> Class but just wanted to figure out why <code>input.useDelimiter()</code> method is not working here.</p>
  
  <p>=====================</p>
  
  <h1>Error Message</h1>
  
  <pre><code>Process started &gt;&gt;&gt;
  &lt;&lt;&lt; Process finished. (Exit code 0)
  java ReadTextFileTest
  Process started &gt;&gt;&gt;
  Account   First Name  Last Name      Balance
  File improperly formed.
  &lt;&lt;&lt; Process finished. (Exit code 1)
  </code></pre>
  
  <p>=====================</p>
  
  <h1>ReadTextFile.java</h1>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.lang.IllegalStateException;
  import java.util.NoSuchElementException;
  import java.util.Scanner;
  
  public class ReadTextFile
  {
     private Scanner input;
  
     public void openFile()
     {
        try
        {
  
           // Orig code: input = new Scanner( new File( "clients.txt")    
           input = new Scanner( new File( "clients.csv")); // added/updated
           input.useDelimiter(",");                          // added 
  
        } 
        catch ( FileNotFoundException fileNotFoundException )
        {
           System.err.println( "Error opening file." );
           System.exit( 1 );
        } 
     } 
  
  
     public void readRecords()
     {
  
        AccountRecord record = new AccountRecord();
  
        System.out.printf( "%-10s%-12s%-12s%10s\n", "Account",
       "First Name", "Last Name", "Balance" );
  
        try 
        {
           while ( input.hasNext() )
           {
              record.setAccount( input.nextInt() ); // read account number
              record.setFirstName( input.next() ); // read first name
              record.setLastName( input.next() ); // read last name
              record.setBalance( input.nextDouble() ); // read balance
  
              System.out.printf( "%-10d%-12s%-12s%10.2f\n",
             record.getAccount(), record.getFirstName(),
             record.getLastName(), record.getBalance() );
           } 
        } 
        catch ( NoSuchElementException elementException )
        {
           System.err.println( "File improperly formed." );
           input.close();
           System.exit( 1 );
        } 
        catch ( IllegalStateException stateException )
        {
           System.err.println( "Error reading from file." );
           System.exit( 1 );
        } 
     } 
  
     public void closeFile()
     {
        if ( input != null )
           input.close(); 
     } 
  } 
  </code></pre>
  
  <p>=====================</p>
  
  <h1>ReadTextFileTest.java</h1>
  
  <pre><code>public class ReadTextFileTest
  {
     public static void main( String args[] )
     {
        ReadTextFile application = new ReadTextFile();
  
        application.openFile();
        application.readRecords();
        application.closeFile();
     } 
  } 
  </code></pre>
  
  <p>=====================</p>
  
  <h1>AccountRecord.java</h1>
  
  <pre><code>public class AccountRecord
  {
     private int account;
     private String firstName;
     private String lastName;
     private double balance;
  
     public AccountRecord() 
     {
        this( 0, "", "", 0.0 );
     } 
  
     public AccountRecord( int acct, String first, String last, double bal)
     {
        setAccount( acct );
        setFirstName( first );
        setLastName( last );
        setBalance( bal );
     } 
     public void setAccount( int acct ) { account = acct; } public int getAccount() { return account; }
  
     public void setFirstName( String first )
     {
        firstName = first;
     } 
     public String getFirstName() 
     { 
        return firstName; 
     } 
     public void setLastName( String last )
     {
        lastName = last;
     } 
     public String getLastName() 
     {
        return lastName; 
     } 
     public void setBalance( double bal )
     {
        balance = bal;
     } 
     public double getBalance() 
     { 
        return balance; 
     } 
  } 
  </code></pre>
  
  <p>===========================</p>
  
  <h1>clients.txt (original file)</h1>
  
  <pre><code>100 Bob Jones 24.98
  200 Steve Doe -345.67
  300 Pam White 0.00
  400 Sam Stone -42.16
  500 Sue Rich 224.62
  100 Bob Jones -4.98    
  200 Steve Doe 45.67
  </code></pre>
  
  <p>===============================================</p>
  
  <h1>clients.csv (change from ".txt" to ".csv" file)</h1>
  
  <pre><code>100,Bob,Jones,24.98
  200,Steve,Doe,-345.67
  300,Pam,White,0.00
  400,Sam,Stone,-42.16
  500,Sue,Rich,224.62
  100,Bob,Jones,-4.98
  200,Steve,Doe,45.67
  </code></pre>