0

I am trying to write the data that I take in via the JOptionPane to a Csv file. Is there a way that I can just write the whole class to it rather than doing it individually?? Cant seem to get it to work

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();
}

And here is the Student Class

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; 
 } 
}
  • Side note: In Java, the naming conventions do not encourage the use of an underscore as the first character of member variable names. – JamesB Nov 19 '13 at 14:04
  • I'm unclear exactly what you're looking for. Is the problem that you want a single *input* to read all of the fields (such as suggested by MouseLearnJava), or do you want a single *output* to write the entire CSV line at once (in which case Christian Kullmann's solution makes sense)? – Ian McLaird Nov 19 '13 at 17:12

2 Answers2

0

You need to override the method

toString()

It will be called automatically when handing the object of class Student to a method which requires a String.

Christian Kullmann
  • 558
  • 1
  • 7
  • 21
0

You can create a JPanel for entering student information.

Like the following mock-up screen shot: ( Left side are the JLabel components, on right side, they are JTextField and JTextArea components )

enter image description here

Once the informaiton are entered, you can create a class to include these values and then write the value into CSV.

In this way, you can enter student informaiton, like name, ID, Email address etc, on one panel, rather than using JOptionPanel, you need to enter student information individually.

Mengjun
  • 3,159
  • 1
  • 15
  • 21