사용자가 이전에 사용한 ID 번호 을 입력하려고하면 레코드를 저장하지 않는 방법은 무엇입니까? while 루프를 사용해 보았습니다. 두 번째로 사용자에게 묻습니다. 그 후에는 작동하지 않습니다. 나는 배열을 사용하는 것에 대해 생각했다?이전에 변수에 숫자가 사용되었는지 확인하는 방법은 무엇입니까?
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();
}
}
}
좋아, 처음에는 잘못된 인상을 가지고 있었다에서. 글쎄, 지금까지 변수 (중복 값의 일종)와 접촉하게 된 것을 추적하여 그렇게하는 유일한 방법. 배열을 선언하는 데 필요한 길이를 알기를 원하면 array가 최소한의 수준에서 도움이된다면 배열을 선언하십시오. 그렇지 않으면 배열, 컬렉션과 같은 더 나은 제어가 필요합니다. 그 다음 그것을 내 씻어 라. – bonCodigo