ユーザーが以前に使用した ID 番号を入力しようとした場合、レコードを保存しないようにするにはどうすればよいですか? while ループを試してみましたが、ユーザーに 2 回目の質問をしても機能しますが、その後は機能しません。配列を使用することを考えましたか?
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();
}
}
}