title: Java - Reading a Text File and Adding a Unique ID
body: <p>I am trying to learn Java and am really struggling on part of a problem that I have.  I am being asked to write a method to read a text file where each line representing an instance of an object e.g. SalesPerson</p>
  
  <p>The question is asking me to add an identifier id for every line that read in, the id is not present in the text file.  I have id declared in my Sales Person class with a constructor and getter and setter methods, and have my method to read the text file below in another class.  However it doesn't work and I am not sure where I am going wrong.  Could someone give me a pointer..?  I would be very grateful.,</p>
  
  <pre><code>public static Collection&lt;SalesPerson&gt; readSalesData() {
      String pathname = CXU.FileChooser.getFilename();
      File aFile = new File(pathname);
      Scanner bufferedScanner = null;
      Set&lt;SalesPerson&gt; salesSet = new HashSet&lt;&gt;();
  
      try {
          int id;
          String name;
          String productCode;
          int sales;
          int years;
          Scanner lineScanner;
          String currentLine;
          bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
  
          while(bufferedScanner.hasNextLine()) {
              currentLine = bufferedScanner.nextLine();
              lineScanner = new Scanner(currentLine);
              lineScanner.useDelimiter(",");
              id = salesPerson.getId();
              name = lineScanner.next(); //return the next token as a string
              years = lineScanner.nextInt();
              productCode = lineScanner.next(); // return the next token as a string
              sales = lineScanner.nextInt(); // return the next token as a double
              salesSet.add(new SalesPerson(id, name, years, productCode, sales));   
          }
      }
      catch (Exception anException) {
          System.out.println("Error: " + anException);
      }
      finally { 
          try {
              bufferedScanner.close();
          }
          catch (Exception anException) {
              System.out.println("Error: " + anException);
          }
      }
      return salesSet;
  }
  
  
  \\Constructor from Class SalesPerson
  public SalesPerson(int aId, String aname, int aYears, String aProductCode, int aSales) {
      super(); // optional
      this.id = ++nextId;
      this.name = aname;
      this.years = aYears;
      this.productCode = aProductCode;
      this.sales = aSales;
  }
  
  
  </code></pre>