[英]How to give a unique ID to each student that is created by the user in a JAVA program
I am working on a project in JAVA where an user types information and the program writes that data in a binary file, you can display students and you can edit students, it all works fine, the only thing I am missing and I am stuck, is there a way that every time the user creates a student, that student will be given a unique ID like 1, 2, 3, etc. I could let the user enter the own ID at creation but they don't know how many students there is in the database already, I found a possible solution but I don't know if that was the best way to do it, look in the code below in the commented section.我正在 JAVA 中开展一个项目,其中用户键入信息,程序将该数据写入二进制文件中,您可以显示学生,也可以编辑学生,一切正常,我唯一缺少的就是我被卡住了,有没有一种方法,每次用户创建学生时,都会为该学生提供一个唯一的 ID,如 1、2、3 等。我可以让用户在创建时输入自己的 ID,但他们不知道有多少学生数据库中已经有,我找到了一个可能的解决方案,但我不知道这是否是最好的方法,请查看下面注释部分的代码。 I would appreciate any help with this.我将不胜感激。 Here is the code.这是代码。 Thanks谢谢
import java.util.Scanner;
import java.io.*;
public class MidTermProject {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Here is the sample of menu choices for Main Menu.");
System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student");
System.out.println("Please enter a valid choice(1, 0 to Quit) :");
int userInput = keyboard.nextInt();
if(userInput == 1) {
CreateStudent();
} else if(userInput == 0) {
System.out.print("Done");
} else {
System.out.println("Invalid Option, Please try again.");
userInput = keyboard.nextInt();
if(userInput == 1) {
CreateStudent();
} else if(userInput == 0) {
System.out.print("Done");
}
public static void CreateStudent() throws IOException {
String FullName;
String address;
String city;
String state;
Scanner keyboard = new Scanner(System.in);
FileOutputStream fstream =
new FileOutputStream("StudentInfo.dat");
DataOutputStream outputFile =
new DataOutputStream(fstream);
System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
FullName = keyboard.nextLine();
outputFile.writeUTF(FullName);
System.out.print("Address: ");
address = keyboard.nextLine();
outputFile.writeUTF(address);
System.out.print("City: ");
city = keyboard.nextLine();
outputFile.writeUTF(city);
System.out.print("State: ");
state = keyboard.nextLine();
outputFile.writeUTF(state);
//allow the user to select its own ID
System.out.print("Please get a Student ID(1-10): ");
StudentID = keyboard.nextInt();
//Used a lop to compare index with student ID and after that index increments
for(int index = 0; index == StudentID; index++) {
//if the ID has been selected, display a message and allow the user to select a different ID
if(index == StudentID) {
System.out.print("The selected ID has been selected already, Please select a different ID");
StudentID = keyboard.nextInt();
}
}
//write the ID in the file
outputFile.writeInt(StudentID);
System.out.print("Successfully Created");
}
System.out.print("Successfully Created");
}
public static void EditStudent() throws IOException {
String editName;
String editaddress;
String editCity;
String editState;
Scanner keyboard = new Scanner(System.in);
RandomAccessFile file =
new RandomAccessFile("StudentInfo.dat", "rw");
file.seek(0);
System.out.print("\nPlease enter NEW information bellow.\n" + "\nFull Name: ");
editName = keyboard.nextLine();
file.writeUTF(editName);
System.out.print("Address: ");
editaddress = keyboard.nextLine();
file.writeUTF(editaddress);
System.out.print("City: ");
editCity = keyboard.nextLine();
file.writeUTF(editCity);
System.out.print("State: ");
editState = keyboard.nextLine();
file.writeUTF(editState);
file.close();
}
public static void DisplayStudent() throws IOException {
FileInputStream fstream = new FileInputStream("StudentInfo.dat");
DataInputStream inputFile = new DataInputStream(fstream);
String student;
boolean endOfFile = false;
//this do not display the ID number that was written in CreateStudent()
while(!endOfFile)
{
try
{
student = inputFile.readUTF();
//Tried the following to display the ID but didn't work
// int StudentID = inputFile.readInt();
System.out.print(student + " ");
}
catch (EOFException e)
{
endOfFile = true;
}
}
System.out.println("\nDone");
inputFile.close();
}
Based on the code, I'm assuming this is a high school or learning assignment.根据代码,我假设这是高中或学习任务。 Then the easiest way to do this is to read in the file, store the highest id in a variable.然后最简单的方法是读取文件,将最高 id 存储在变量中。 When the user adds in a new student, add 1 to that variable and use that as his id.当用户添加一个新学生时,将 1 添加到该变量并将其用作他的 id。
In a more advanced application, you'd use a UUID, which is basically a very large random id that would be seriously unlikely to be replicated.在更高级的应用程序中,您将使用 UUID,它基本上是一个非常大的随机 id,极不可能被复制。
add a static variable on student class, and increment it on constructor (so that it will be incremented every time you create a new student) ex:在学生 class 上添加一个 static 变量,并在构造函数上增加它(这样每次创建新学生时它都会增加)例如:
private static int studentID = 1;
public Student()
{
studentID++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.