Java Program Input from file, output to file(s)

In summary: Parses the population from the text file.*//*** Parses the population from the text file and displays it in the jlabel.*//*** Creates a new jlabel and sets its text.*//*** Sets the default close operation for the form.*//*** Sets up the form's group layout.*//*** Called from within the constructor to initialize the form.*//*** Handles the action events for the various buttons on the form.*/}
  • #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:

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
 
Physics news on Phys.org
  • #2


What's your question?
Do you have a clear idea of the algorithm you're going to implement?
 
  • #3


OOPS, forgot to put the problem I was having, LOL :rolleyes:

I intended this section of code:

Code:
/**
 * This program reads a text file line by line. 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();
      
**HERE IS WHERE I THINK THE CORRECT CODE NEEDS TO BE TO BUILD THE ARRAYLIST**
      
      }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

to read the file into an array list. Then, using the cityRecord class, and a for/each loop that I haven't created yet, determine which city goes to BigCity.txt and which city goes to SmallCity.txt.

Right now I am having an issue figuring out how to read the file in and get the information to populate cityRecord correctly.
 
  • #4


Are you planning to use GUI for this or the console? If you're using a GUI, then how are you planning to call the ReadFileText?

I haven't deal a lot with BufferedInputStream or any of the stuffs noted above. What I like to used to read files is Scanner. So this could be another way for you to read the file.

To read a file with the Scanner class:
Code:
Scanner scanner = null;
		try{
			String line = null;
			scanner = new Scanner(new File("filename"));
			while (scanner.hasNextLine()){ //Checks if there's any more line
				line = scanner.nextLine(); //Read the line
				//Parse line and do your stuffs
			}
		} catch (Exception e){
			e.printStackTrace();
		}

However, if you're still interested in using DataInputStream, let me know, I can look it up.
 
  • #5


I am planning on using a GUI. I will call the ReadTextFile with one button. Once all records are read in, another button would then separate the records into two files depending on population. I will try the scanner option that sourlemon recommended. Will the space in between city & state, and between state & population already be used as a deliminator, or do I have to specify it somehow?
 
  • #6


OK, new code as follows:

GUI:
Code:
package phase4db2;

/*
 * 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 phase4db2.CityRecord;
import java.io.*;
import java.util.*;

/**
 *
 * @author Jon and Jessica
 */
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;
        cityName = ReadTextFile.city;
        stateName = ReadTextFile.state;
        populationNum = ReadTextFile.population;

         CityRecord cityRecord = new CityRecord(cityName, stateName, populationNum);

        // 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

}

ReadTextFile Class

Code:
package phase4db2;
import java.io.File;
import java.util.Scanner;

/**
 * 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) {

      String city;
      String state;
      int population;
      
      Scanner scanner = null;
		try{
			String line = null;
			scanner = new Scanner(new File("population.txt"));
                        while (scanner.hasNextLine()){ //Checks if there's any more line
				line = scanner.nextLine(); //Read the line
                        
                           city = scanner.next(); 
                           state = scanner.next();
                           population = scanner.nextInt();
            //**Problem here??** *  I think I am using scanner wrong to get my values?
            // I get an error saying cannot find variable in ReadTextFile.java but I have      //declared the variables and attempted to get the value from the text file.
                       
			}
		} catch (Exception e){
			e.printStackTrace();
		}
  }
}

and CityRecord class

Code:
package phase4db2;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jon and Jessica
 */
// Fig. 17.5: CityRecord.java
// CityRecord class maintains information for one account.
 // packaged for reuse

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000, 
 * 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.
 */

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

I changed over to the scanner method for pulling information from the text file.
 
  • #7


I think you have some problems here:
Code:
public static void main(String[] args)
{

  String city;
  String state;
  int population;
      
  Scanner scanner = null;
  try
  {
     String line = null;
     scanner = new Scanner(new File("population.txt"));
     while (scanner.hasNextLine())
     { 
        //Checks if there's any more line
        line = scanner.nextLine(); //Read the line
                        
        city = scanner.next(); 
        state = scanner.next();
        population = scanner.nextInt();
        //**Problem here??** *  I think I am using scanner wrong to get my values?
        // I get an error saying cannot find variable in ReadTextFile.java but I have      
        //declared the variables and attempted to get the value from the text file.
     }
  }

  catch (Exception e)
  {
    e.printStackTrace();
  }
}
I believe you're trying to do more input than you really want to. I don't have any way to test my belief here, but it seems to me that scanner.nextLine() is picking up the city, then city = scanner.next() is picking up the state, and then state = scanner.next() is picking up a string with the population. The next input is expecting an int, but you have already read it, and are instead reading a string of characters rather than a number.

Another thing that should be fixed is that you have a cityRecordList array of your CityRecord structs. You should get rid of that. All you need is one cityRecord instance that you use over and over again.

The basic algorithm should be:
While there is more data in the input file
Read the input file to fill the fields of a CityRecord struct.
Write the CityRecord fields to the output file

Finally, try to use reasonable indentation. In your code, in ReadTextFile(), you had
Code:
      Scanner scanner = null;
		try{

The try statement should be aligned with the statement just above it. It should not be indented. The basic idea is that statements that execute in sequence should be aligned the same.
 
  • #8


Ok, I have attempted to clean up the indentation :) Sorry, it is an issue with me, LOL.

Also, I got rid of the ReadTextFile class and put the methods in the GUI buttons themselves, seemed like this would work out better. It is starting to act like it wants to work right, but now when I run the program, I get an error that says it can't find the input file.

New Code as follows:

GUI Class:
Code:
package phase4db2;

/*
 * 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
 */

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000,
 * 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.
 */

import java.io.*;
import java.util.*;

/**
 *
 * @author Jon and Jessica
 */
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
        Scanner scanner = null;
      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine()){ //Checks if there's any more line
	String city = scanner.next();
        String state = scanner.next();
        int population = scanner.nextInt();

         // create a CityRecord object
        CityRecord cityRecord = new CityRecord(city, state, population);

        // Add elements to array list
        cityRecordList.add(cityRecord);
	}
	}
        catch (Exception e)
        {
			e.printStackTrace();
	}
    }                                            

    // Create an array list that can grow as data is input by user
    ArrayList<CityRecord> cityRecordList = new ArrayList<CityRecord>();

    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
        FileOutputStream bOut; // declare a file output object
        FileOutputStream sOut; // declare a second file output object
        PrintStream p1; // declare a print stream object
        PrintStream p2; // declare second print stream object
        try
        {
             // Create a new file output stream
             // connected to "population.txt"
             bOut = new FileOutputStream("BigCities.txt");
             sOut = new FileOutputStream("SmallCities.txt");

             // Connect print stream to the output stream
             p1 = new PrintStream( bOut );
             p2 = new PrintStream( sOut );

             // Print heading for page
             p1.println ("Population by City and State");
             // Print hyphenated line for dilineator
             p1.println("----------------------------");
             // For each record in the cityRecordList ArrayList, print a record
             for (CityRecord rec : cityRecordList)
             {
                if ( rec.population >= 50000)
                {
                    p1.println(rec);
                }
                else
                {
                    // Print heading for page
                    p2.println ("Population by City and State");
                    // Print hyphenated line for dilineator
                    p2.println("----------------------------");

                    p2.println(rec);
                }

             }

                p1.close();
                p2.close();
         }
         catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }

              

       

    }                                               
   
    /**
    * @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                   

}

And I am trying to run everything with just the CityRecord class since it worked so very well with my last project.

CityRecord class:

Code:
package phase4db2;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jonathan Smith
 */

// CityRecord class maintains information for one account.
 // packaged for reuse

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000,
 * 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.
 */
public class CityRecord
{

   String city;
   String state;
   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

When I click the Import File button I get the following error:

run:
java.io.FileNotFoundException: population.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at phase4db2.PopulationInput.importButtonActionPerformed(PopulationInput.java:131)
at phase4db2.PopulationInput.access$100(PopulationInput.java:28)
at phase4db2.PopulationInput$2.actionPerformed(PopulationInput.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Any suggestions?
 
  • #9


Also, input file comes from a simple text file (population.txt) created in the src folder that uses space deliminated data as follows

Example:

Jamestown FL 50000
Domville CO 10000
Jennyboro FL 30000
Lillyville CO 5000
 
  • #10


The population.txt file should be in the same directory as the compiled executable, which is probably different from where the source code is.

When you get this running, you should get rid of cityRecordList. You don't need an array, so it just complicates things to no advantage, thereby making it harder to understand.

Also, try to get your indent formatting fixed. It's mostly OK, but you have something like this in your parseFileButtonActionPerformed method.
Code:
try{
   scanner = new Scanner(new File("population.txt"));
   while (scanner.hasNextLine()){ //Checks if there's any more line
        
     // while loop body
   }
   }
        catch (Exception e)
        {
			e.printStackTrace();
        }
The catch statement should be aligned with its try statement, not indented. And you should never have two closing braces stacked up with one directly under the preceding one. This is probably icing on the cake, but if you have things properly indented, it makes it much easier to understand the flow of control.
 
  • #11


OK, figured out where the input file goes. It was just in the wrong place, thanks Mark44.

Also, I fixed the indentation.

When you get this running, you should get rid of cityRecordList. You don't need an array, so it just complicates things to no advantage, thereby making it harder to understand.

I assumed I would need the array to hold the data for CityRecord class. If I get rid of the array, how do I send the data to the CityRecord constructor? I am a little confused about how this works. Is it going to send line by line and print as it goes?

I also have to make sure that if BigCities.txt and SmallCities.txt already exist, it doesn't overwrite the file, it appends to the file. Once I finish cleaning the code, and get rid of the array, I will work on that final step.

Code as follows:

GUI class:
Code:
package phase4db2;

/*
* 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
*/

/*Develop an application that reads city, state, and population from a text file.
* If the population of the city is greater than or equal to 50,000,
* 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.
*/

import java.io.*;
import java.util.*;

/**
*
* @author Jon and Jessica
*/
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
Scanner scanner = null;
try{
scanner = new Scanner(new File("population.txt"));
while (scanner.hasNextLine())
{ //Checks if there's any more line
String city = scanner.next();
String state = scanner.next();
int population = scanner.nextInt();

// create a CityRecord object
CityRecord cityRecord = new CityRecord(city, state, population);

// Add elements to array list
cityRecordList.add(cityRecord);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

// Create an array list that can grow as data is input by user
ArrayList<CityRecord> cityRecordList = new ArrayList<CityRecord>();

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
FileOutputStream bOut; // declare a file output object
FileOutputStream sOut; // declare a second file output object
PrintStream p1; // declare a print stream object
PrintStream p2; // declare second print stream object
try
{
// Create new file output streams
bOut = new FileOutputStream("BigCities.txt"); // output stream for large cities
sOut = new FileOutputStream("SmallCities.txt"); // output stream for small cities

// Connect print streams to the output stream
p1 = new PrintStream( bOut );
p2 = new PrintStream( sOut );

// Print heading for Big Cities page
p1.println ("Population by City and State");
// Print hyphenated line for dilineator
p1.println("----------------------------");
// For each record in the cityRecordList ArrayList, print a record
// Print heading for Small Cities page
p2.println ("Population by City and State");
// Print hyphenated line for dilineator
p2.println("----------------------------");
// For each record in the cityRecordList ArrayList, print a record

for (CityRecord rec : cityRecordList)
{
if ( rec.population >= 50000)
{
p1.println(rec);
}
else
{

p2.println(rec);
}

}

p1.close();
p2.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}





}

/**
* @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

}

CityRecord class:
Code:
package phase4db2;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jonathan Smith
 */

// CityRecord class maintains information for one account.
 // packaged for reuse

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000,
 * 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.
 */
public class CityRecord
{

   String city;
   String state;
   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

   public String toString() {
   return (city + " " + state + " " + population);
   }

} // end class CityRecord
 
Last edited:
  • #12


No, you don't need an array to hold the single CityRecord instance that you need. Having an array of these structures adds another (and unneeded) layer.

You don't need to keep all of the data in memory (i.e., in an array) for each city, state, and population that you read. All you need is a single CityRecord instance that you re-use over and over.

Here's an analogous problem. Suppose you need to write a program that reads ints from an input file and determines which of them is larger than 10.

You don't need to store all of the ints in an array. All you need is one int variable.

The algorithm is:
While the input file contains data
Read an int into a variable n.
If n > 10, display a message on the screen.
End while
 
  • #13


Ok, I can understand how that works.

Code:
 private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // When Import File button is clicked, import the file specified in the text field
        Scanner scanner = null;
      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                String city = scanner.next(); // reads to first space in line, sets the variable
                String state = scanner.next(); // reads to second space in line, sets the variable
                int population = scanner.nextInt(); // reads to the next space in line, sets variable

                               
            }
	}
        catch (Exception e)
        {
			e.printStackTrace();
	}
    }

I got rid of the array. Now I need to pass the variables to the CityRecord constructor somehow right?
 
  • #14


another issue with getting rid of the array is that I was using the array to determine population to decide which city went to which file.

Code:
private void parseFileButtonActionPerformed(java.awt.event.Acti onEvent evt) {
// When Parse File by Population button is pressed, send data to text files
FileOutputStream bOut; // declare a file output object
FileOutputStream sOut; // declare a second file output object
PrintStream p1; // declare a print stream object
PrintStream p2; // declare second print stream object
try
{
// Create new file output streams
bOut = new FileOutputStream("BigCities.txt"); // output stream for large cities
sOut = new FileOutputStream("SmallCities.txt"); // output stream for small cities

// Connect print streams to the output stream
p1 = new PrintStream( bOut );
p2 = new PrintStream( sOut );

// Print heading for Big Cities page
p1.println ("Population by City and State");
// Print hyphenated line for dilineator
p1.println("----------------------------");
// For each record in the cityRecordList ArrayList, print a record
// Print heading for Small Cities page
p2.println ("Population by City and State");
// Print hyphenated line for dilineator
p2.println("----------------------------");
// For each record in the cityRecordList ArrayList, print a record

for (CityRecord rec : cityRecordList)
{
if ( rec.population >= 50000)
{
p1.println(rec);
}
else
{

p2.println(rec);
}

}

p1.close();
p2.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}





}

If I get rid of the array, I need a new way of checking the population
 
  • #15


iamjon.smith said:
Ok, I can understand how that works.

Code:
 private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // When Import File button is clicked, import the file specified in the text field
        Scanner scanner = null;
      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                String city = scanner.next(); // reads to first space in line, sets the variable
                String state = scanner.next(); // reads to second space in line, sets the variable
                int population = scanner.nextInt(); // reads to the next space in line, sets variable

                               
            }
	}
        catch (Exception e)
        {
			e.printStackTrace();
	}
    }

I got rid of the array. Now I need to pass the variables to the CityRecord constructor somehow right?

No. You need only one CityRecord instance, so its constructor should be called just once.

You have methods on CityRecord that you can assign values to the three members using setCity, setState, setPopulation.
 
  • #16


iamjon.smith said:
If I get rid of the array, I need a new way of checking the population
This is very simple to do.
Instead of this:
Code:
for (CityRecord rec : cityRecordList)
{
   if ( rec.population >= 50000)
   {
      p1.println(rec);
   }
   else
   {
      p2.println(rec);
   }
}

do this:
Code:
if ( rec.population >= 50000)
{
   p1.println(rec);
}
else
{
   p2.println(rec);
}

I am assuming that rec is the single CityRecord instance.
 
  • #17


I tried using the setCity() method from CityRecord, but when I done this:

Code:
 private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // When Import File button is clicked, import the file specified in the text field
        Scanner scanner = null;
      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                String city = scanner.next(); // reads to first space in line, sets the variable
                String state = scanner.next(); // reads to second space in line, sets the variable
                int population = scanner.nextInt(); // reads to the next space in line, sets variable

                CityRecord.setCity( city );  // *error
                CityRecord.setState( state ); // *error
                CityRecord.setPopulation ( population ); // *error

                
            }
	}
        catch (Exception e)
        {
			e.printStackTrace();
	}
    }

where the *error is, I get an error saying non-static method setCity(java.lang.String) cannot be referenced from a static context. Changes to setPopulation(int) for population of course.

As for this:
I am assuming that rec is the single CityRecord instance.

rec comes from

Code:
for (CityRecord rec : cityRecordList)

It IS a single instance, but it is for each instance in the array. I get rid of the above line of code and rec is no longer usable as is.
 
  • #18


Ok, got the first part figured out I believe:

Code:
private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // When Import File button is clicked, import the file specified in the text field
        Scanner scanner = null;
      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                String city = scanner.next(); // reads to first space in line, sets the variable
                String state = scanner.next(); // reads to second space in line, sets the variable
                int population = scanner.nextInt(); // reads to the next space in line, sets variable

                CityRecord cityRecord = new CityRecord();

                cityRecord.setCity( city);
                cityRecord.setState( state );
                cityRecord.setPopulation ( population );            }
	}
        catch (Exception e)
        {
			e.printStackTrace();
	}
    }

Now I just need to figure out how to make the loop work for dividing the cities by population.
 
  • #19


Compare this to what you have. Note that I am declaring cityRecord, city, state, and population only once, not once per loop iteration.
Code:
private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
   // When Import File button is clicked, import the file specified in the text field
   Scanner scanner = null;
   CityRecord cityRecord = new CityRecord();
   String city, state;
   int population;

   try
   {
       scanner = new Scanner(new File("population.txt"));
       while (scanner.hasNextLine())
       {
          //Checks if there's any more lines
          city = scanner.next(); // reads to first space in line, sets the variable
          state = scanner.next(); // reads to second space in line, sets the variable
          population = scanner.nextInt(); // reads to the next space in line, sets variable

          // CityRecord cityRecord = new CityRecord();

          cityRecord.setCity( city);
          cityRecord.setState( state );
          cityRecord.setPopulation ( population );

         if (population > 50000)
         {
             // Write data to BigCities file
          }
          else
          {
             // Write data to other file
          }
       }
   }
   catch (Exception e)
   {
      e.printStackTrace();
   }
 }
 
  • #20


Ok, with the way you have it, there is no need for the parse file button. Everything is done with the click of a single button. It reads the file in, then loops through and divides the cities by population. That is fine, I can do it that way.
However, you dumped all of the following code:

Code:
FileOutputStream bOut; // declare a file output object
FileOutputStream sOut; // declare a second file output object
PrintStream p1; // declare a print stream object
PrintStream p2; // declare second print stream object
try
{
// Create new file output streams
bOut = new FileOutputStream("BigCities.txt"); // output stream for large cities
sOut = new FileOutputStream("SmallCities.txt"); // output stream for small cities

// Connect print streams to the output stream
p1 = new PrintStream( bOut );
p2 = new PrintStream( sOut );

would all of this be included in the print loop, or is it just not needed?
 
  • #21


All I did was modify the code you had in post #18, so I didn't "dump" the stuff you have in post #20.

Those declarations would still be needed, but you should not put them in the while loop, otherwise your program will be creating many more of these objects than it needs. I would put these declarations at the beginning of importButtonActionPerformed.
 
  • #22
Ok, I understand what you did. I have modified my code, and here it is:

Code:
package phase4db2;

/*
 * 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
 */

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000,
 * 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.
 */

import java.io.*;
import java.util.*;

/**
 *
 * @author Jon and Jessica
 */
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();
        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");

        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(189, Short.MAX_VALUE)
                .addComponent(quitButton)
                .addGap(182, 182, 182))
        );
        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)
                .addGap(52, 52, 52)
                .addComponent(quitButton)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        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
        Scanner scanner = null;
        FileOutputStream bOut; // declare a file output object
        FileOutputStream sOut; // declare a second file output object
        PrintStream p1; // declare a print stream object
        PrintStream p2; // declare second print stream object

        String city, state;
        int population;

        CityRecord cityRecord = new CityRecord();

      try{
	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                city = scanner.next(); // reads to first space in line, sets the variable
                state = scanner.next(); // reads to second space in line, sets the variable
                population = scanner.nextInt(); // reads to the next space in line, sets variable
             
                cityRecord.setCity( city);
                cityRecord.setState( state );
                cityRecord.setPopulation ( population );
                
                // Create new file output streams
                bOut = new FileOutputStream("BigCities.txt"); // output stream for large cities
                sOut = new FileOutputStream("SmallCities.txt"); // output stream for small cities

                // Connect print streams to the output stream
                p1 = new PrintStream( bOut );
                p2 = new PrintStream( sOut );

                // Print heading for Big Cities page
                p1.println ("Population by City and State");
                // Print hyphenated line for dilineator
                p1.println("----------------------------");
                // Print heading for Small Cities page
                p2.println ("Population by City and State");
                // Print hyphenated line for dilineator
                p2.println("----------------------------");


                if(population >= 50000)
                {
                    p1.println(city + " " + state + " " + population);
                }
                else
                {

                    p2.println(city + " " + state + " " + population);
                }


                p1.close();
                p2.close();
         }
      }
         catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }
    }                                            


    
    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // When Quit button is pressed, exit application
        System.exit(0);
    }                                          
   
    /**
    * @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 quitButton;
    // End of variables declaration

}

Cityrecord has not changed.

Now everything works to some extent, but I must have missed something. The input file is imported, and the two output files are created, but it only populates data for the last item in the input file instead of every time. What did I miss?
 
  • #23
GOT IT! I was closing the files to early. Here is the working code with the correct output.

Code:
package phase4db2;

/*
 * 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
 */

/*Develop an application that reads city, state, and population from a text file.
 * If the population of the city is greater than or equal to 50,000,
 * 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.
 */

import java.io.*;
import java.util.*;

/**
 *
 * @author Jon and Jessica
 */
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();
        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");

        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(189, Short.MAX_VALUE)
                .addComponent(quitButton)
                .addGap(182, 182, 182))
        );
        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)
                .addGap(52, 52, 52)
                .addComponent(quitButton)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        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
        Scanner scanner = null;
        FileOutputStream bOut; // declare a file output object
        FileOutputStream sOut; // declare a second file output object
        PrintStream p1; // declare a print stream object
        PrintStream p2; // declare second print stream object

        String city, state;
        int population;

        CityRecord cityRecord = new CityRecord();

      try{

         // Create new file output streams
        bOut = new FileOutputStream("BigCities.txt"); // output stream for large cities
        sOut = new FileOutputStream("SmallCities.txt"); // output stream for small cities

        // Connect print streams to the output stream
        p1 = new PrintStream( bOut );
        p2 = new PrintStream( sOut );

        // Print heading for Big Cities page
        p1.println ("Population by City and State");
        // Print hyphenated line for dilineator
        p1.println("----------------------------");
        // Print heading for Small Cities page
        p2.println ("Population by City and State");
        // Print hyphenated line for dilineator
        p2.println("----------------------------");

	scanner = new Scanner(new File("population.txt"));
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                city = scanner.next(); // reads to first space in line, sets the variable
                state = scanner.next(); // reads to second space in line, sets the variable
                population = scanner.nextInt(); // reads to the next space in line, sets variable
             
                cityRecord.setCity( city);
                cityRecord.setState( state );
                cityRecord.setPopulation ( population );

                if(population >= 50000)
                {
                    p1.println(city + " " + state + " " + population);
                }
                else
                {

                    p2.println(city + " " + state + " " + population);
                }
         }
                p1.close();
                p2.close();
      }
         catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }
    }                                            


    
    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // When Quit button is pressed, exit application
        System.exit(0);
    }                                          
   
    /**
    * @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 quitButton;
    // End of variables declaration

}

Go over this and see if you see any problems. If not the only thing I need to work on is if the files exist, append to them instead of overwriting every time.
 
  • #24
This is the only thing I see - slightly goofy indentation.
Code:
  .
  .
  .
  try
  {
     .
     .
     .
     scanner = new Scanner(new File("population.txt"));
     while (scanner.hasNextLine())
     { 
         //Checks if there's any more lines
         city = scanner.next(); // reads to first space in line, sets the variable
         state = scanner.next(); // reads to second space in line, sets the variable
         population = scanner.nextInt(); // reads to the next space in line, sets variable
             
         cityRecord.setCity( city);
         cityRecord.setState( state );
         cityRecord.setPopulation ( population );

         if(population >= 50000)
         {
             p1.println(city + " " + state + " " + population);
         }
         else
         {
             p2.println(city + " " + state + " " + population);
         }
     }

     p1.close();
     p2.close();
  }

  catch (Exception e)
  {
      System.err.println ("Error writing to file");
  }
}
This indentation makes it clear at a glance that within the try block, a scanner is created, the while loop starts (and finishes), and p1 and p2 are closed. All of those statements should be at the same indentation level.
 
  • #25
OK, LOL, :) I obviously need to spend some time working on proper indentation. Having said that, here is the final question, and I think I have it right already:

here is the new change to the code:

Code:
// Create new file output streams
        bOut = new FileOutputStream("BigCities.txt", true); // output stream for large cities with boolean value for appending to file
        sOut = new FileOutputStream("SmallCities.txt" , true); // output stream for small cities with boolean value for appending to file

by adding the boolean value "True" to the FileOutputStream constructors, my program will now append to a file if it exists. That is what understand from my text and online research. To the best of your knowledge, have I got that right?
 
  • #27
Do you control the formatting input of the file? If yes, I would consider another way to separate the city from the state. What happen when you come across "New York City" in "New York?" Maybe use a comma or tab to separate them. But if that is never going to happen, then ignore me. :)
 
  • #28
sourlemon said:
Do you control the formatting input of the file? If yes, I would consider another way to separate the city from the state. What happen when you come across "New York City" in "New York?" Maybe use a comma or tab to separate them. But if that is never going to happen, then ignore me. :)
That's a good point. There are quite a few cities whose names are two words: St. Louis, Kansas City, Rapid City, Des Moines, and so on.
 
  • #29
I realized the merit of what Mark44 and sourlemon said and tried to use a tab delimiter with the scanner class, but now I my program throws an exception error:

Error writing to file.

I think I may have the delimiter misplaced or something. Here is where I changed the code:

Code:
try{

        // Create new file output streams
        bOut = new FileOutputStream("BigCities.txt", true); // output stream for large cities with boolean value for appending to file
        sOut = new FileOutputStream("SmallCities.txt" , true); // output stream for small cities with boolean value for appending to file

        // Connect print streams to the output stream
        p1 = new PrintStream( bOut );
        p2 = new PrintStream( sOut );

        
	scanner = new Scanner(new File("population.txt"));
        
        while (scanner.hasNextLine())
            { //Checks if there's any more lines
                scanner.useDelimiter("/t");  // delimiter here to read tabbed input
                city = scanner.next(); // reads to first space in line, sets the variable
                state = scanner.next(); // reads to second space in line, sets the variable
                population = scanner.nextInt(); // reads to the next space in line, sets variable
             
                cityRecord.setCity( city);
                cityRecord.setState( state );
                cityRecord.setPopulation ( population );
               

                if(population >= 50000)
                {
                    p1.println(city + " " + state + " " + population);
                }
                else
                {

                    p2.println(city + " " + state + " " + population);
                }
         }
                p1.close();
                p2.close();
      }
     catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }
    }

If it makes a difference, I just deleted the space between city & state, and state & population and hit the tab key in the appropriate places in the population.txt file.
 
  • #30
The tab character is '\t', so the string you pass in useDelimiter should be "\t".
 
  • #31
even switching to the proper slash "\t" as follows:

Code:
scanner = new Scanner(new File("population.txt"));
        scanner.useDelimiter("\t");
        while (scanner.hasNextLine())
            { //Checks if there's any more lines         
                city = scanner.next(); // reads to first space in line, sets the variable
                state = scanner.next(); // reads to second space in line, sets the variable
                population = scanner.nextInt(); // reads to the next space in line, sets variable
                //...more code

I still get the error writing to file.

I am still missing something.
 
  • #32
I believe your problem has to do with the pattern you're using to separate the city, state, and population fields, and this pattern seems to have something to do with regular expressions. See Pattern class.

The useDelimiter() method you're using has an overload that takes a Pattern parameter, and it would seem that the two methods operate similarly.

Do you have a debugger to use? If so, see what city is set to in the line, city = scanner.next();
Same for state and population.

If you don't have a debugger or don't know how to use it, add some temporary lines of code to display the values of these three variables to the console.

What sort of write error do you get?
 

1. How do I input data from a file in a Java program?

To input data from a file in a Java program, you can use the FileInputStream class to open the file and read its contents. You can then use a BufferedReader to read the data line by line.

2. How do I output data to a file in a Java program?

To output data to a file in a Java program, you can use the FileOutputStream class to create a new file or overwrite an existing one. You can then use a BufferedWriter to write the data to the file.

3. Can I input and output data to multiple files in a Java program?

Yes, you can input and output data to multiple files in a Java program by using multiple instances of FileInputStream and FileOutputStream, and by specifying the file paths for each file.

4. How do I handle errors when inputting or outputting data to files in a Java program?

You can use try-catch blocks to handle errors when inputting or outputting data to files in a Java program. This allows you to catch any exceptions that may occur and handle them appropriately, such as displaying an error message or closing the file streams.

5. Are there any libraries or APIs that can make inputting and outputting data to files easier in Java?

Yes, there are libraries and APIs such as Apache Commons IO and Java NIO (New I/O) that provide classes and methods for easier and more efficient input and output operations on files in Java. These can be useful for handling large files or performing more complex operations on files.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
27
Views
23K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
10K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
5K
Back
Top