By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

reading a .txt file from the command line but no main method

P: 5
I have project I have to do for class. We have to write 4 different .java files.

I already finish and I need to work on both the and Here are the requirements for those two.

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

Expand|Select|Wrap|Line Numbers
  1. //
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JTextArea;
  9. import javax.swing.JTextField;
  11. public class HouseGUI extends JFrame
  12. {
  13.    final static int MAX_NUMBER_OF_NUMBERS = 100;
  14.    House[] Home = new House[MAX_HOUSES];
  15.    private JTextArea textAreaOriginal;
  16.    private JTextArea textAreaSorted;
  17.    private JTextField messageField;
  19.    /**
  20.     * Creates this window
  21.     */
  22.    public HouseGUI()
  23.    {
  24.       // where will we input the args[0]
  25.       String inputFileName = args[0]; 
  27.       setSize(400, 275);
  28.       setLocation(100, 100);
  29.       setTitle("The List of Houses and the Sorted List of Houses.");
  31.       Container contentPane = getContentPane();
  33.       JPanel panel = new JPanel();
  34.       panel.setLayout(new GridLayout(1, 2));
  35.       contentPane.add(panel, BorderLayout.CENTER);
  37.       textAreaOriginal = new JTextArea();
  38.       textAreaOriginal.setEditable(false);
  39.       panel.add(new JScrollPane(textAreaOriginal));
  41.       textAreaSorted = new JTextArea();
  42.       textAreaSorted.setEditable(false);
  43.       panel.add(new JScrollPane(textAreaSorted));
  45.       messageField = new JTextField();
  46.       messageField.setEditable(false);
  47.       contentPane.add(messageField, BorderLayout.SOUTH);
  49.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.       setVisible(true);
  52.       int readFile(inputFileName,Home);
  54.       displayNumbers(textAreaOriginal);
  56.       displayNumbers(textAreaOriginal);
  57.    }  //constructor
  59.    public static int readFile(String filename,House[] houses)
  60.    {
  61.        int lengthFilled = 0;
  62.        TextFileInput in = new TextFileInput(filename);
  64.        // Read capacities into array:
  66.        //reads the lines of txt file into the array
  68.        String line = in.readLine();   // read first line in file
  69.        while ( lengthFilled < houses.length && line != null )
  70.                {
  71.                    line = in.readLine();  // read next line in file
  72.                    if(houses.isValidHouse(line)== false)
  73.                        {
  74.                        System.out.println(line + " is an incorrect line in the txt file.");
  75.                        }
  76.                    if(houses.isValidHouse(line) == true)
  77.                    houses[lengthFilled++] = line;
  78.                    line = in.readLine();
  79.                } // while
  81.            // Check to see if all the capacities in the file were read.
  82.            // If not, then the array wasn't big enough to hold them all.
  83.            // In that case, print an error message and quit.
  84.            if ( line != null )           
  85.            {
  86.                    System.out.println("File contains too much capacity.");
  87.                    System.out.println("This program can process only "
  88.                            + houses.length + " capacities.");
  89.                    System.exit(1);
  90.            }  // if
  92.            // Release file for re-use:
  93.            in.close();
  95.            return lengthFilled;       
  96.    }// readFile method
  98.            private void displayNumbers(JTextArea textArea)
  99.         {
  100.            String lineBreak = System.getProperty("line.separator");
  101.            for ( int i = 0; i < lengthFilled; i++ )
  102.            {
  103.               textArea.append(Double.toString(numbers[i]));
  104.               textArea.append(lineBreak);
  105.            } // for
  106.         }  // method displayNumbers
  109. }// HouseGUI 
Here's my file

Expand|Select|Wrap|Line Numbers
  1. //
  5. public class Project2 
  6. {
  8.      public static void main(String[] args)
  9.         {
  10.          final static int MAX_HOUSES = 100;
  11.          House[] Houses = new House[MAX_HOUSES];
  12.          HouseGUI GUI = new HouseGUI(); 
  13.         }// main
  15. } // class House
btw I've also attached a .txt for a better explanation of the requirements.
Thank you for everyone's help.
Attached Files
File Type: txt Program Description.txt (8.3 KB, 411 views)
Oct 29 '08 #1
Share this Question
Share on Google+
4 Replies

P: 188

You have a main method inside the Project2 class, you should use that to read in your file name. When running your program from the command line you will be able to pass the file name argument just the same way you pass arguments to other programs. Remeber GUI programs can be run from the command line just like console programs. Once you have the file name, then just use it as you wish.
you may also check whether the user passed the file name, if it's not there, just complain and exit...

About the error, it only means that the compiler can't find the method you are trying to use. Do you have it? If so, is it correctly spelled?


Oct 29 '08 #2

P: 5
Actually I fumbled with the and I actually set the name file that i'm suppose to read as a string and I called the readFile method from the HouseGUI class in the file. I"m not sure if I can do that or am I suppose to call the the name file from in the

btw the class name should be correct. I just wanted to declare the class House as an array by typing:

Expand|Select|Wrap|Line Numbers
  1. House[] Home = new House[MAX_HOUSES];
it thinks I'm calling class House[] rather than House.
Oct 30 '08 #3

P: 13,264
houses is an array of houses not a House. You need to call isValid... on a house object not on the array itself.
Oct 30 '08 #4

P: 5
Thank you very much everyone. I solved the problem thank you. I was not suppose to declare a new House class in the HouseGUI class. :]
Oct 31 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.