Project2.java
HouseGUI.java
House.java
HouseSorting.java
I already finish House.java and I need to work on both the Project2.java and HouseGUI.java Here are the requirements for those two.
The GUI
Create a class called HouseGUI which extends JFrame. It should display two text areas (JTextArea) in a grid layout (1 row, 2 columns). Your main program, Project2.java, will contain only a main method which does nothing except instantiate an object of class HouseGUI (which should not have a main method. The GUI should use the first text area to display the contents of the original array, before sorting, and the second one to display the array contents sorted in ascending order by price. Be sure to make the JFrame wide enough to display everything.
Creating the Array
In House.java, declare a static constant (final variable) called MAX_HOUSES and set it equal to 100.(which i did) In either the main method or the constructor of class Project2, declare a local array reference for the array of House items and instantiate it using the size MAX_HOUSES. Read a file name from the command line. Your Project 2 GUI class (HouseGUI) should have a method readFile with the following heading:
public static int readFile (String filename, House[] houses)
which opens the file, reads one House string from each line of the file, checks if it's valid,tokenizes it, instantiates a House, and inserts the House into the array. As in Project 1, this array will be partially filled (the file may not contain 100 lines) so the program will need to keep track of how many Houses were read from the file and put in the array. The readFile method should return the number of Houses that have actually been read into the array. Lines representing invalid Houses (which may be detected via a call to the public
isValidHouse method of class House) should be sent to the console window in an error message.
Ok my question is how do I read in a file from the command line when there is no main method for me to retrieve the argument?
When I try to call on a method from the house class, I get an error:
cannot find symbol
symbol " method isValidHouse<java.lang.String>
location: class House[]
and a number of other errors
Here is my code for the HouseGui.java
Expand|Select|Wrap|Line Numbers
- //HouseGUI.java
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.GridLayout;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class HouseGUI extends JFrame
- {
- final static int MAX_NUMBER_OF_NUMBERS = 100;
- House[] Home = new House[MAX_HOUSES];
- private JTextArea textAreaOriginal;
- private JTextArea textAreaSorted;
- private JTextField messageField;
- /**
- * Creates this window
- */
- public HouseGUI()
- {
- // where will we input the args[0]
- String inputFileName = args[0];
- setSize(400, 275);
- setLocation(100, 100);
- setTitle("The List of Houses and the Sorted List of Houses.");
- Container contentPane = getContentPane();
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(1, 2));
- contentPane.add(panel, BorderLayout.CENTER);
- textAreaOriginal = new JTextArea();
- textAreaOriginal.setEditable(false);
- panel.add(new JScrollPane(textAreaOriginal));
- textAreaSorted = new JTextArea();
- textAreaSorted.setEditable(false);
- panel.add(new JScrollPane(textAreaSorted));
- messageField = new JTextField();
- messageField.setEditable(false);
- contentPane.add(messageField, BorderLayout.SOUTH);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- int readFile(inputFileName,Home);
- displayNumbers(textAreaOriginal);
- displayNumbers(textAreaOriginal);
- } //constructor
- public static int readFile(String filename,House[] houses)
- {
- int lengthFilled = 0;
- TextFileInput in = new TextFileInput(filename);
- // Read capacities into array:
- //reads the lines of txt file into the array
- String line = in.readLine(); // read first line in file
- while ( lengthFilled < houses.length && line != null )
- {
- line = in.readLine(); // read next line in file
- if(houses.isValidHouse(line)== false)
- {
- System.out.println(line + " is an incorrect line in the txt file.");
- }
- if(houses.isValidHouse(line) == true)
- houses[lengthFilled++] = line;
- line = in.readLine();
- } // while
- // Check to see if all the capacities in the file were read.
- // If not, then the array wasn't big enough to hold them all.
- // In that case, print an error message and quit.
- if ( line != null )
- {
- System.out.println("File contains too much capacity.");
- System.out.println("This program can process only "
- + houses.length + " capacities.");
- System.exit(1);
- } // if
- // Release file for re-use:
- in.close();
- return lengthFilled;
- }// readFile method
- private void displayNumbers(JTextArea textArea)
- {
- String lineBreak = System.getProperty("line.separator");
- for ( int i = 0; i < lengthFilled; i++ )
- {
- textArea.append(Double.toString(numbers[i]));
- textArea.append(lineBreak);
- } // for
- } // method displayNumbers
- }// HouseGUI
Expand|Select|Wrap|Line Numbers
- // Project2.java
- public class Project2
- {
- public static void main(String[] args)
- {
- final static int MAX_HOUSES = 100;
- House[] Houses = new House[MAX_HOUSES];
- HouseGUI GUI = new HouseGUI();
- }// main
- } // class House
Thank you for everyone's help.