-3

How would I go about not saving the record if the user tries to enter a previously used ID number? I have tried a while loop, tho it works for the second time asking the user, it wont work after that. I thought about using an array?

import java.util.*;

import java.io.*;
public class CreateCustomerFile
{
   public static void main(String[] args) throws IOException
   {
      int pos;
      RandomAccessFile it = 
         new RandomAccessFile("CustomerFile.txt", "rw");
      Scanner input = new Scanner(System.in);

      int id;
      String name;
      int zip;
      final int RECORDSIZE = 100;
      final int NUMRECORDS = 1000;
      final int STOP = 99;
      try
      {


        byte [] padding = new byte[RECORDSIZE];
     Arrays.fill(padding, (byte)0 );
     for(int x = 0; x < NUMRECORDS; ++x)
     {
         it.write(padding);
     }

      }

      catch(IOException e)
      {
         System.out.println("Error: " + e.getMessage());
      }
      finally
      {
         it.close();
      }
      it = 
        new RandomAccessFile("CustomerFile.txt","rw");
      try
      {
         System.out.print("Enter ID number" +  
                        " or " + STOP + " to quit ");

         id = input.nextInt();
         while(id != STOP)
         {
            input.nextLine();

            System.out.print("Enter last name");
            name = input.nextLine();
                while(name.length() > 7 || name.length() <7)
                {   
                System.out.print("Name must be 7 characters; Enter last name");
            name = input.nextLine();
                }
            System.out.print("Enter zipcode ");
            zip = input.nextInt();
            pos = id - 1;
            it.seek(pos * RECORDSIZE);
            it.writeInt(id);
                it.writeUTF(name);
            it.writeInt(zip);
                System.out.print("Enter ID number"  +
               " or " + STOP + " to quit ");
           int id1 = input.nextInt();

                if(id == id1)
                {
             System.out.print("Id has been previously used" );

                 System.out.print("Enter ID number"  +
               " or " + STOP + " to quit ");
           id1 = input.nextInt();

                }
                id = id1;

                }

         }  

      catch(IOException e)
      {
         System.out.println("Error: " + e.getMessage());
      }
      finally
      {
         it.close();
      }
   }
   }
Sam
  • 7,252
  • 16
  • 46
  • 65
user1067332
  • 15
  • 2
  • 6
  • Alright, at first I had the wrong impression. Well, the only way to do so by tracking what has come to contact with the variable so far (sort of duplicate values). To say, store it ---> if array could help you at minimum level, hoping you know the length you need to declare the array for otherwise some better control like a ditionary, collection. Then flush it off. – bonCodigo Nov 26 '12 at 03:34

2 Answers2

2

You should consider using UUID's for unique identifiers.

Boo Radley
  • 684
  • 1
  • 13
  • 25
0

Sounds like you might need a hashtable/dictionary.

Java's hashtable/dictionary implementation can be found:

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Dictionary.html

Basic Example:

import java.util.*;
public class DictionaryDemo {
public static void main(String[] args)
{
Dictionary d=new Hashtable();

    d.put("1", "a");
    d.put("2","b" );
    d.put("3","ck" );
    d.put("4","c" );
    d.put("10","d" );
    System.out.println(d.get("10"));
    System.out.println(d.remove("10")+"  has been removed");
    System.out.println("the  value of key 10 = " +d.get("10"));

 for (Enumeration e = d.keys();e.hasMoreElements();)
     System.out.println(e.nextElement());
     System.out.println(e.nextElement());

}
}
Jon
  • 1,055
  • 13
  • 24