title: How to make integer value auto-increment in java?
body: <p>I have a text file which has records like this:</p>
  
  <pre><code>1 Hamada PEPSI
  2 Johny PEPSI
  </code></pre>
  
  <p>the format of these records is like this:</p>
  
  <pre><code>int id, String name, String drink
  </code></pre>
  
  <p>I wrote a small method to add records to this text file but id must be unique for every record</p>
  
  <p>for example:
  these records are not acceptable:</p>
  
  <pre><code>1 Hamada PEPSI
  2 Johny PEPSI
  1 Terry Milk
  </code></pre>
  
  <p>here is my code:</p>
  
  <pre><code>public void addProduct(int id, String name, String drink)
  {
  Formatter x = null;
  try{
  FileWriter f = new FileWriter("C:\\Users\\فاطمة\\Downloads\\products.txt", true);
  x = new Formatter(f);
  x.format("%d %s %s %s%n",id,name,drink);
  x.close();
  }
  catch(Exception e)
  {
  System.out.println("NO Database");
  }
  }
  </code></pre>
  
  <p>How to make id be auto-increment when entering new record?</p>
  
  <p>for example: </p>
  
  <pre><code>1 Ahmed PEPSI
  2 Hamada PEPSI
  3 Johny Milk
  4 Terry Milk
  5 Jack Miranda
  6 Sarah Juice
  </code></pre>