使用jframe我正在尝试创建一个学生列表,将它们保存到一个文件中,重新读取所有这些学生并创建新的学生,当且仅当他们的ID号不相同时。如果ID号相同,我应该收到一条错误消息,说明该ID已被使用,因此无法注册新学生。这里唯一的问题是,即使已经使用了ID,它也会注册学生。我做错了什么?
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class NewEstudiantesJFrame extends javax.swing.JFrame {
public static List <Estudiantes> EstReg = new ArrayList<>();
public static Long ci, ciprueba;
public NewEstudiantesJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void AceptarButtonActionPerformed(java.awt.event.ActionEvent evt) {
Estudiantes estu = new Estudiantes();
try {
FileInputStream fis = new FileInputStream("t.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
EstReg = (List<Estudiantes>) ois.readObject();
ois.close();// es necesario cerrar el input stream
} catch (IOException | ClassNotFoundException ex) {
}
String NumId = new String();
String tipoId = (String) TipoIdBox.getSelectedItem();
estu.TipoId = tipoId;
NumId = NumIdField.getText();
if ((NumId.length()>9)||(NumId.length()<8)){
JOptionPane.showMessageDialog(null, "Please Type in the 8 or 9 ID digits");
a++;
NumIdField.setText(null);
//NumIdField.requestFocusInWindow();
}
else
{ ci = Long.parseLong(NumId);
}
try{
**for (Estudiantes e : EstReg){
if (e.NumId == Long.parseLong(NumId)){
JOptionPane.showMessageDialog(null, "ID already in use, please check your data");
NumIdField.setText(null);
NumIdField.requestFocusInWindow();
}
else {
estu.NumId = ci;
}**
}
}
catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Inpult only ID numbers");
a++;
}
这是我的Estudiantes课程
import java.io.Serializable;
public class Estudiantes implements Serializable{
String Nombre;
String Apellido;
String Direccion;
String Email;
String CursoActual;
String TipoId;
Long NumId;
String IdTotal;
String CodTel;
Long NumTel;
}
由于
答案 0 :(得分:0)
不确定这是否有效,但在比较两个长片而不是==时尝试使用.compareTo()。然后检查结果值是否等于零。 https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#compareTo-java.lang.Long-
答案 1 :(得分:0)
抱歉我的不好,我刚才意识到我没有显示剩下的代码......真的很抱歉。我不得不使用同一文件的先前版本并使用.compareTo选项。感谢Bryan Herrera的链接。
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class NewEstudiantesJFrame extends javax.swing.JFrame {
public static List <Estudiantes> EstReg = new ArrayList<>();
public NewEstudiantesJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void AceptarButtonActionPerformed(java.awt.event.ActionEvent evt) {
Estudiantes estu = new Estudiantes();
try {
FileInputStream fis = new FileInputStream("C:\\t.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
EstReg = (List<Estudiantes>) ois.readObject();
ois.close();
} catch (IOException | ClassNotFoundException ex) {
}
String NumId = new String();
String nombre = new String();
String numTel = new String();
nombre = NombreField.getText();
String apellido = ApellidoField.getText();
String direccion = DireccionArea.getText();
String codtel;
boolean curso;
curso = false;
int a = 0;
String tipoId = (String) TipoIdBox.getSelectedItem();
estu.TipoId = tipoId;
if ((nombre.length()== 0)|| apellido.isEmpty()){
JOptionPane.showMessageDialog(null, "Please type in students full name");
a++;
}
else{
estu.Nombre = NombreField.getText();
estu.Apellido = ApellidoField.getText();
if (direccion.length()== 0){
JOptionPane.showMessageDialog(null, "Please input Student's address");
a++;
}
else{
estu.Direccion = DireccionArea.getText();}
}
try {
NumId = NumIdField.getText();
if ((NumId.length()!=8)){
JOptionPane.showMessageDialog(null, "Please type in the 8 digits if your ID");
a++;
NumIdField.setText(null);
else{
estu.NumId = Long.parseLong(NumId);
}
}
catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Inpult only ID numbers");
a++;
}
for (Estudiantes es: EstReg){
if (es.NumId.compareTo(estu.NumId)==0){
JOptionPane.showMessageDialog(null,"ID already in use, please check your data");
NumIdField.setText(null);
a++;
}
}
try {
numTel = NumTelField.getText();
if (numTel.length()!=7){
JOptionPane.showMessageDialog(null, "Please type in the 7 telephone number digits");
a++;
}
else
estu.NumTel = Long.parseLong(numTel);
}
catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Error. Only accepts numbers");
NumTelField.setText(null);
a++;
}
if (jRadioButton1.isSelected()){
estu.CursoActual= "Maths 1";
curso = true;
}
if (jRadioButton2.isSelected()){
estu.CursoActual ="Maths 2";
curso = true;
}
if (jRadioButton3.isSelected()){
estu.CursoActual ="Maths 3";
curso = true;
}
if ((curso == false) || (a > 0)){
JOptionPane.showMessageDialog(null, "Please finish filling the form out");
}
else{
ObjectOutputStream oos = null;
try {
JOptionPane.showMessageDialog(null, "Student succesfully registered. Thank you!");
estu.IdTotal = tipoId + NumId;
EstReg.add(estu);
FileOutputStream fos = new FileOutputStream("C:\\t.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(EstReg);
oos.close();
this.dispose();
} catch (Exception ex) {
}
}
}