I have written a program student record system but can not save data to view or submit record.
Able to run the program but failed to submit record or view pre-submitted records.
Student.java
import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; class Student extends Frame implements ActionListener { String greeding=""; Button btnNewStudent; Button btnSubmit; Button btnView; Label lblStudentName; Label lblStudentAge; Label lblStudentAddr; Label lblStudentGender; Label lblProgram; TextField txtStudentName; TextField txtStudentAge; TextArea txtStudentAddr; TextArea txtAns; CheckboxGroup ChkGrp; Checkbox chkMale,chkFemale; Checkbox chkGameDesign,chkComputerStudies,chkHotelManagement,chkArtAndDesign; Student(String Studentname) { super(Studentname); setLayout(new GridLayout(3,2)); lblStudentName = new Label("Name: "); lblStudentAge = new Label("Age: "); lblStudentAddr = new Label("Address : "); lblStudentGender = new Label("Gender: "); lblProgram = new Label("Qualification: "); txtStudentName = new TextField(20); txtStudentAge = new TextField(20); txtStudentAddr = new TextArea(); ChkGrp = new CheckboxGroup(); chkMale = new Checkbox("Male",ChkGrp,false); chkFemale = new Checkbox("Female",ChkGrp,false); chkGameDesign = new Checkbox("Game Design"); chkComputerStudies = new Checkbox("Computer Studies"); chkHotelManagement = new Checkbox("Hotel Management"); chkArtAndDesign = new Checkbox("Art and Design"); btnNewStudent = new Button("NEW"); btnSubmit = new Button("SUBMIT"); btnView = new Button("VIEW"); btnNewStudent.addActionListener(this); btnSubmit.addActionListener(this); btnView.addActionListener(this); add(lblStudentName); add(txtStudentName); add(lblStudentAge); add(txtStudentAge); add(lblStudentAddr); add(txtStudentAddr); add(lblStudentGender); add(chkMale); add(chkFemale); add(lblProgram); add(chkGameDesign); add(chkComputerStudies); add(chkHotelManagement); add(chkArtAndDesign); add(btnNewStudent); add(btnSubmit); add(btnView); txtAns = new TextArea(); add(txtAns); } public void actionPerformed(ActionEvent ae) { String s=""; boolean b; FileInputStream Fin; DataInputStream dis; FileOutputStream Fout; DataOutputStream dos; try { Fout = new FileOutputStream("Biodata.txt",true); dos = new DataOutputStream(Fout); String str = ae.getActionCommand(); if(str.equals("SUBMIT")) { s=txtStudentName.getText().trim(); dos.writeUTF(s); dos.writeInt(Integer.parseInt(txtStudentAge.getText())); s=txtStudentAddr.getText(); dos.writeUTF(s); if(chkMale.getState()) dos.writeUTF("Male "); if(chkFemale.getState()) dos.writeUTF("Female "); s=""; if(chkGameDesign.getState()) s="Game Design "; if(chkComputerStudies.getState()) s+="Computer Studies "; if(chkHotelManagement.getState()) s+="Hotel Management "; if(chkArtAndDesign.getState()) s+="Art And Design "; s+="!"; dos.writeUTF(s); Fout.close(); } if(str.equals("VIEW")) { String tmp,name,addr,gender,qual; int age; Fin = new FileInputStream("Biodata.txt"); dis = new DataInputStream(Fin); int i=0,j; while(Fin.available()>0) { name = dis.readUTF(); age = dis.readInt(); addr = dis.readUTF(); gender = dis.readUTF(); qual = dis.readUTF(); if(name.equals(txtStudentName.getText().trim())) { txtStudentAge.setText(age+""); txtStudentAddr.setText(addr); if(gender.equals("Male ")) chkMale.setState(true); else chkFemale.setState(true); while(Program.charAt(i)!='!') { j=qual.indexOf(' '); tmp = qual.substring(i,j); if(tmp.equals("Game Design")) chkGameDesign.setState(true); if(tmp.equals("Computer Studies")) chkComputerStudies.setState(true); if(tmp.equals("Hotel Management")) chkHotelManagement.setState(true); if(tmp.equals("Art And Design")) chkArtAndDesign.setState(true); i=j+1; } break; } } Fin.close(); } if(str.equals("NEW")) { txtStudentName.setText(""); txtStudentAge.setText(""); txtStudentAddr.setText(""); chkMale.setState(false); chkFemale.setState(false); chkGameDesign.setState(false); chkComputerStudies.setState(false); chkHotelManagement.setState(false); chkArtAndDesign.setState(false); } } catch(Exception e) { System.out.println("The Exception Is : " +e); } } }
Record.java
class Record { public static void main (String args[]) { try{ Student F = new Student("Record"); F.setSize(400,400); F.show(); }catch(Exception e) { System.out.println(e); } } }