我正在尝试将我通过JOptionPane获取的数据写入Csv文件。有没有办法让我可以把整个班级写成它而不是单独做?似乎无法开始工作
import javax.swing.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class AddStudent implements Serializable
{
public static void main(String[] args) throws IOException
{
Student s1 = new Student();
String id = JOptionPane.showInputDialog(null, "Enter Students ID ");
s1.setStudentName(JOptionPane.showInputDialog(null, "Enter Students name "));
s1.setStudentNumber(JOptionPane.showInputDialog(null, "Enter Students ID "));
s1.setStudentEmail(JOptionPane.showInputDialog(null, "Enter Students Email Address "));
s1.setStudentAdd(JOptionPane.showInputDialog(null, "Enter Students Home Address "));
String ProgID = JOptionPane.showInputDialog(null, "Enter the Programme code the student is taking ") ;
FileWriter fw = new FileWriter("C:\\Users\\Packard Bell\\Desktop\\ProjectOOD\\ProgrammeID.csv", true);
}
FileOutputStream fos = new FileOutputStream("ProgrammeID");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Student s1 = new Student();
oos.writeObject( s1);
oos.close();
}
这是学生班
class Student
{
private String _studentNumber;
private String _studentName;
private String _studentAddress;
private String _studentEmail;
public void setStudentName(String studentName)
{
_studentName = studentName;
}
public void setStudentNumber(String studentNumber)
{
_studentNumber = studentNumber;
}
public void setStudentAdd(String studentAddress)
{
_studentAddress = studentAddress;
}
public void setStudentEmail(String studentEmail)
{
_studentEmail = studentEmail;
}
public String getNumber()
{
return _studentNumber;
}
public String getName()
{
return _studentName;
}
public String getAddress()
{
return _studentAddress;
}
public String getEmail()
{
return _studentAddress;
}
}
答案 0 :(得分:0)
您需要覆盖方法
toString()
将类Student的对象传递给需要String的方法时,将自动调用它。
答案 1 :(得分:0)
您可以创建一个JPanel来输入学生信息。
类似于以下模拟屏幕截图:(左侧是JLabel组件,右侧是JTextField和JTextArea组件)
输入信息后,您可以创建一个类来包含这些值,然后将值写入CSV。
通过这种方式,您可以在一个面板上输入学生信息,如姓名,ID,电子邮件地址等,而不是使用JOptionPanel,您需要单独输入学生信息。