- #1
iamjon.smith
- 117
- 3
Develop an application that reads city, state, and population from a text file.
Example input census file:
Jamestown FL 50000
Domville CO 10000
Jennyboro FL 30000
Lillyville CO 5000
If the population of the city is greater than or equal to 50,000, write the city information to a text file called BigCities.txt. If the population of the city is less than 50,000, write the city information to a text file called SmallCities.txt.
If your output file(s) already exist, you should append to the file and not overwrite it.
Ok, I know I am part of the way there. I have the following code:
Main class is a jform GUI:
I also have two subclasses. There is nothing that says I have to use these. I just thought I would need both of them.
ReadTextFile Class
and the City Record class
Example input census file:
Jamestown FL 50000
Domville CO 10000
Jennyboro FL 30000
Lillyville CO 5000
If the population of the city is greater than or equal to 50,000, write the city information to a text file called BigCities.txt. If the population of the city is less than 50,000, write the city information to a text file called SmallCities.txt.
If your output file(s) already exist, you should append to the file and not overwrite it.
Ok, I know I am part of the way there. I have the following code:
Main class is a jform GUI:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* PopulationInput.java
*
* Created on Feb 23, 2011, 11:37:31 AM
*/
import java.io.*;
import java.util.*;
/**
*
* @author Jonathan
*/
public class PopulationInput extends javax.swing.JFrame {
/** Creates new form PopulationInput */
public PopulationInput() {
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();
inputFileName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
parseFileButton = new javax.swing.JButton();
importButton = new javax.swing.JButton();
quitButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Population Input");
inputFileName.setText("population.txt");
jLabel2.setText("Input File");
parseFileButton.setText("Parse File By Population");
parseFileButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
parseFileButtonActionPerformed(evt);
}
});
importButton.setText("Import File");
importButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importButtonActionPerformed(evt);
}
});
quitButton.setText("Quit");
quitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quitButtonActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(188, 188, 188)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(165, 165, 165)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(inputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(importButton)))))
.addContainerGap(158, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(92, Short.MAX_VALUE)
.addComponent(parseFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(71, 71, 71))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(197, Short.MAX_VALUE)
.addComponent(quitButton)
.addGap(174, 174, 174))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(36, 36, 36)
.addComponent(jLabel2)
.addGap(13, 13, 13)
.addComponent(inputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(importButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
.addComponent(parseFileButton)
.addGap(41, 41, 41)
.addComponent(quitButton)
.addGap(23, 23, 23))
);
pack();
}// </editor-fold>
private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {
// When Import File button is clicked, import the file specified in the text field
}
private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// When Quit button is pressed, exit application
System.exit(0);
}
private void parseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
// When Parse File by Population button is pressed, send data to text files
// create a CityRecord object
String cityName;
String stateName;
int populationNum;
CityRecord cityRecord = new CityRecord();
// Add elements to array list
cityRecordList.add(cityRecord);
}
// Create an array list that can grow as data is input by user
ArrayList<CityRecord> cityRecordList = new ArrayList<CityRecord>();
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PopulationInput().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton importButton;
private javax.swing.JTextField inputFileName;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton parseFileButton;
private javax.swing.JButton quitButton;
// End of variables declaration
}
I also have two subclasses. There is nothing that says I have to use these. I just thought I would need both of them.
ReadTextFile Class
Code:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class ReadTextFile {
public static void main(String[] args) {
File file = new File("population.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
CityRecord cityRecord= new CityRecord();
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and the City Record class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jonathan
*/
// Fig. 17.5: CityRecord.java
// CityRecord class maintains information for one account.
// packaged for reuse
public class CityRecord
{
private String city;
private String state;
private int population;
// no-argument constructor calls other constructor with default values
public CityRecord()
{
this( "", "", 0 ); // call three-argument constructor
} // end no-argument CityRecord constructor
// initialize a record
public CityRecord( String city, String state, int population )
{
setCity( city );
setState( state );
setPopulation( population );
} // end three-argument CityRecord constructor
// set city name
public void setCity( String cityName )
{
city = cityName;
} // end method setCity
// get city name
public String getCity()
{
return city;
} // end method getCity
// set state name
public void setState( String stateName )
{
state = stateName;
} // end method setState
// get state name
public String getState()
{
return state;
} // end method getState
// set population
public void setPopulation( int pop )
{
population = pop;
} // end method setPopulation
// get Population
public double getPopulation()
{
return population;
} // end method getPopulation
} // end class CityRecord