aspose file tools*
The moose likes I/O and Streams and the fly likes Problem with writting in File... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with writting in File..." Watch "Problem with writting in File..." New topic
Author

Problem with writting in File...

Muhammad Aasim
Greenhorn

Joined: Oct 16, 2009
Posts: 9
Problem Statements:
You are required to develop a simple GUI (Graphical user interface) application for the registration of students in an institution. The program should be able to store the student data into a text file and when the user starts up the application again then previously stored data of student must also be saved into text file. The program should be able to handle events such as ActionEvent and ItemEvent.
The class Student must have following attributes:

• Student Id
• Student name
• Student father’s name
• Student age
• Phone number
• Study Program (Combo Box)


The GUI must have labels, text boxes, combo box and buttons as shown in the figure below. The title of application must be displayed within center at the top of application.


In the above GUI application, when the user clicks ‘Register’ button then it will take the data from the form and store into text file. The application must quit by clicking ‘Exit’ button.

My Code 4 this Problem

import java.util.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;

public class studentRegForm implements ActionListener{
JFrame regFrame;
JLabel srf, sID,sName,sFName,sPNum,sAge,sDProg;
JTextField tsID,tSName,tsFName,tsPNum,tsAge;
JComboBox cProg;
JButton bReg,bExit;

public void GUIGenerator(){

regFrame=new JFrame("MC080405905");
srf=new JLabel("Student Registration Form",JLabel.CENTER);
srf.setPreferredSize(new Dimension(350,20));

sID=new JLabel("Student ID:");
sID.setPreferredSize(new Dimension(100,20));
sName=new JLabel("Student Name:");
sName.setPreferredSize(new Dimension(100,20));
sFName=new JLabel("Father's Name:");
sFName.setPreferredSize(new Dimension(100,20));
sPNum=new JLabel("Phone Number:");
sPNum.setPreferredSize(new Dimension(100,20));
sAge=new JLabel("Student Age:");
sAge.setPreferredSize(new Dimension(100,20));
sDProg=new JLabel("Degree Program:");
sDProg.setPreferredSize(new Dimension(100,20));

tsID=new JTextField(25);
tSName=new JTextField(25);
tsFName=new JTextField(25);
tsPNum=new JTextField(25);
tsAge=new JTextField(25);

bReg=new JButton("Register");
bReg.setPreferredSize(new Dimension(120,20));
bExit=new JButton("Exit");
bExit.setPreferredSize(new Dimension(80,20));
cProg=new JComboBox();
cProg.addItem("BBA");
cProg.addItem("BCS");
cProg.addItem("MBA");
cProg.addItem("MCS");

Container cStd=regFrame.getContentPane();
cStd.setLayout(new FlowLayout());

cStd.add(srf);
cStd.add(sID);
cStd.add(tsID);
cStd.add(sName);
cStd.add(tSName);
cStd.add(sFName);
cStd.add(tsFName);
cStd.add(sPNum);
cStd.add(tsPNum);
cStd.add(sAge);
cStd.add(tsAge);
cStd.add(sDProg);
cStd.add(cProg); //combo Box along with items
cStd.add(bReg);
cStd.add(bExit);

bReg.addActionListener(this); //Registering
bExit.addActionListener(this); //Registering the event

regFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
regFrame.setSize(400,330);
regFrame.setVisible(true);
}

//constructor
public studentRegForm()
{

GUIGenerator();
}


public void actionPerformed(ActionEvent event)
{
String iD, name,fName,pNum,age,entry;
JComboBox prog;

if(event.getSource()==bExit)
{

System.exit(1);
}

else if(event.getSource()==bReg)
{

try{

iD=tsID.getText();
tsID.setText("");
name=tSName.getText();
tSName.setText("");
fName=tsFName.getText();
tsFName.setText("");
pNum=tsPNum.getText();
tsPNum.setText("");
age=tsAge.getText();
tsAge.setText("");
// prog=cProg.getSelectedItem();
entry=iD+","+name+","+fName+","+pNum+","+age;

FileWriter fw=new FileWriter("student Data Base.txt");
PrintWriter pw=new PrintWriter(fw);

pw.println(entry);
pw.flush();
pw.close();
fw.close();

}catch(IOException ioex)
{
System.out.println(ioex);
}
}
}

public static void main(String args[])
{
studentRegForm std=new studentRegForm();
}
}

Problem in Code
I am unable to write at the end of file.. whenever i register 1 record it clears the first one.
i am also unable to get data from the Combo Box..
Please Help in this regard.....

EveRy THinG is pOssiBle bUt LovE caN NaveR FOr Me...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

"Bwp Guy",

Please read your private messages. Also, please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Hi,

just make changes in the line

FileWriter fw=new FileWriter("student Data Base.txt");
to
FileWriter fw=new FileWriter("student Data Base.txt", true);

it will open the file if exists and append data to it. Just give it a try.
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
hi,
for combo box problem
change the datatype of prog from JComboBox prog; to String Prog
and add this line

prog=(String)cProg.getSelectedItem();

it works fine.
Muhammad Aasim
Greenhorn

Joined: Oct 16, 2009
Posts: 9
Thanks all of you 4 helping me...
Muhammad Aasim
Greenhorn

Joined: Oct 16, 2009
Posts: 9
Rob Prime wrote:"Bwp Guy",

Please read your private messages. Also, please Use Code Tags.


I have changed my Name...
I am new in the forum so i was unable to find the code tag...
Within few days i will cover all the rules and i will follow it...
Your guidelines are required at every step...
Thanks..
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
When you are posting... Just look at the top of the Text Area where you type your message. You would see a "Code" button. Just highlight the code with your mouse and click the button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with writting in File...