Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 避免重复对象属性_Java - Fatal编程技术网

Java 避免重复对象属性

Java 避免重复对象属性,java,Java,使用jframe,我试图创建一个学生列表,将他们保存到一个文件中,重新读取所有这些学生,并在他们的id号不相同的情况下创建新的学生。如果ID号相同,我应该会收到一条错误消息,说明该ID已在使用,因此无法注册新学生。这里唯一的问题是,即使ID已经被使用,它也会注册学生。我做错了什么 import java.io.*; import java.util.*; import javax.swing.JOptionPane; public class NewEstudiantesJFrame ext

使用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++;
    }

谢谢

我不确定这是否可行,但在比较两个长时,请尝试使用.compareTo,而不是==。然后检查结果值是否等于零。
对不起,我的不好,我刚刚意识到我没有显示其余的代码。。。真的很抱歉。我必须使用同一文件的早期版本并使用.compareTo选项。感谢布莱恩·赫雷拉的链接

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) {

        }
    }
}                                

序列化在这个问题上没有任何明显的作用。或者Swing,或者ArrayList。重写equals方法并使用EstReg.containse。在这里看不到写入文件。它是否得到实施?如果是,请在阅读后尝试打印列表。最初,列表将变为空。读取并初始化列表后,请执行空检查。@user3330990您没有显示如何实例化EStudintes对象的实现。问题就在那里。您还必须显示文件的读取方式。你的代码中有很多问题,只有在你发布了所有信息后,我才能给出答案。
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) {

        }
    }
}