- #1
iamjon.smith
- 117
- 3
Develop an application that reads city, state and population entered from the user and writes this information to a file. The application should continuously prompt for this population information until terminated by the user.
Example file:
Jamestown FL 50000
Domville CO 10000
Jennyboro FL 30000
Blountsville CO 5000
Ok, first of all, I have decided to use a GUI for this project, which is optional, but I feel should be the appropriate format because that is what most applications will require in real world programming. I have most of the form working. It has a clear button, quit button, Enter Data Button, and Print to file button:
I know where the code needs to go, but I don't know a whole lot about the java IO stream. When the user clicks on the Enter Data button, I need the text in the text fields (city, state, & population) to be set and the form cleared. I guess an array would be the best way to store the information. The user should be able to continue to enter data as long as there is data to be entered, and then upon clicking the Print To File button, a text file should be created that displays the data in a format similar to the example file above.
Example file:
Jamestown FL 50000
Domville CO 10000
Jennyboro FL 30000
Blountsville CO 5000
Ok, first of all, I have decided to use a GUI for this project, which is optional, but I feel should be the appropriate format because that is what most applications will require in real world programming. I have most of the form working. It has a clear button, quit button, Enter Data Button, and Print to file button:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* populationForm.java
*
* Created on Feb 16, 2011, 12:06:59 PM
*/
/**
*
* @author Jon and Jessica
*/
public class populationForm extends javax.swing.JFrame {
/** Creates new form populationForm */
public populationForm() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
city = new javax.swing.JTextField();
state = new javax.swing.JTextField();
population = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Population File Creator");
jLabel2.setText("City:");
jLabel3.setText("State:");
jLabel4.setText("Population:");
jButton1.setText("Clear Form");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Create File");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Quit");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Enter Data ");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(140, 140, 140)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(city, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
.addComponent(state)
.addComponent(population))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(10, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(population, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3))
.addGap(38, 38, 38)
.addComponent(jButton4)
.addContainerGap(32, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
city.setText("");
state.setText("");
population.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
city.getText();
state.getText();
population.getText();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new populationForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField city;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField population;
private javax.swing.JTextField state;
// End of variables declaration
}
I know where the code needs to go, but I don't know a whole lot about the java IO stream. When the user clicks on the Enter Data button, I need the text in the text fields (city, state, & population) to be set and the form cleared. I guess an array would be the best way to store the information. The user should be able to continue to enter data as long as there is data to be entered, and then upon clicking the Print To File button, a text file should be created that displays the data in a format similar to the example file above.