Я застрял с этим кодом, я получаю много ошибок, какое-то время пытался их исправить, я думаю, что это незначительные ошибки, но я все еще не могу их понять. Количество записей, которые я ' м используется 50 студентов, и номера студентов будут ключевыми записями. Структура записи: целое число. Строка имени. Адресная строка. Код: Спасибо.
// first create a container class to hold structure of Student record
public class StudentRecord {
int StudentNumber;
String StudentName;
String StudentAddress;
// initializer or constructor
public StudentRecord(id, name, address) {
StudentNumber = id;
StudentName = name;
StudentAddress = address;
}
}
/* now a class that holds file manipulation methods to add,search,delete records with a file using hashing technique */
import java.util.*;
import java.io.*;
public class SimpleSerializedIO {
/* method to add record to file()->creates hash table->load a student record to hash table
->opens/creates file object->inserts/Save record->closes file stream */
private static void SaveRecord() {
System.out.println("|nsert,Save Method |");
e
System.out.println();
Hashtable < String, Object > h = new Hashtable < String, Object > ();
// create student record with roll no,name and address
// roll number of Student is assumed unique key here which is hashed on put() call o insert
h.put("38", new StudentRecord(38, "StudentName1", "StudentAddress1"));
try {
System.out.println("Creating File/Object output stream...");
FileOutputStream fileOut = new FileOutputStream("StudentRecords.txt"); // provide actual full path
ObjectOutputStream out = new ObjectOutputStream(fileOut);
System.out.println("Inserting/Saving Hashtable Object...");
out.writeObject(h);
System.out.println("Closing all output streams after nsert/Save operation...\n");
out.close();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* read records and search method()->open Student record file->load all records into an empty hash table
->close file->query hash table by key input and print search data */
private static void ReadAllRecordsAndSearch(String studentNumber) {
System.out.println("| Search Method |");
System.out.println();
Hashtable h = null;
try {
System.out.println("Creating File/Object input stream...");
FileInputStream fileIn = new FileInputStream("StudentRecords.txt"); // provide actual full path
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println("Loading Hashtable Object...");
h = (Hashtable) in .readObject();
System.out.println("Closing all input streams...\n"); in .close();
fileIn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Printing out Searched element...");
// this loop is just for illustration.You can avoid it and directly query or search the hash table when key is passed.
for (Enumeration e = h.keys(); e.hasMoreElements();) {
Object obj = e.nextElement();
// check if key in context reached?here key is StudentNumber
if (h.get(studentNumber).StudentNumber.toString() == studentNumber) {
System.out.println(" - Element(" + obj + ") = " + h.get(obj));
}
}
System.out.println();
}
/* delete a record after loading all from file into hash table */
public void DeleteRecord(String studentNumber) {
// /////////////////////
System.out.println("| Delete Method |");
System.out.println();
Hashtable h = null;
try {
System.out.println("Creating File/Object input stream...");
FileInputStream fileIn = new FileInputStream("StudentRecords.txt"); // provide actual full path
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println("Loading Hashtable Object...");
h = (Hashtable) in .readObject();
System.out.println("Closing all input streams...\n"); in .close();
fileIn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Printing out deleted element...");
// this loop is just for illustration.You can avoid it and directly query to delete from hash table when key is passed.
for (Enumeration e = h.keys(); e.hasMoreElements();) {
Object obj = e.nextElement();
// check if key in context reached?here key is StudentNumber
if (h.get(studentNumber).StudentNumber.toString() == studentNumber) {
System.out.println(" - Deleting Element(" + obj + ") = " + h.get(obj));
h.remove(studentNumber);
}
}
System.out.println();
// ////////////////////
}
// driver code to test and demo
public static void main(String[] args) {
SaveRecord();
ReadAllRecordsAndSearch("38");
DeleteRecord("38");
}
}