• Support PF! Buy your school textbooks, materials and every day products Here!

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

  • Comp Sci
  • Thread starter iamjon.smith
  • Start date
  • #1
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
 

Answers and Replies

  • #2
33,086
4,793


What's your question?
Do you have a clear idea of the algorithm you're going to implement?
 
  • #3
117
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
78
1


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
117
3


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
117
3


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
33,086
4,793


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
117
3


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
117
3


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
33,086
4,793


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
117
3


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
33,086
4,793


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
117
3


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
117
3


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
33,086
4,793


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
33,086
4,793


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
117
3


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
117
3


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
33,086
4,793


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
117
3


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
33,086
4,793


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
117
3
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
117
3
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
33,086
4,793
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
117
3
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?
 

Related Threads for: Java Program Input from file, output to file(s)

Replies
27
Views
21K
Replies
0
Views
1K
  • Last Post
Replies
6
Views
2K
Replies
1
Views
1K
Replies
3
Views
561
Replies
6
Views
1K
Replies
15
Views
677
Replies
6
Views
2K
Top