472,794 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

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

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

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.

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
  1. //HouseGUI.java
  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 Project2.java file

Expand|Select|Wrap|Line Numbers
  1. // Project2.java
  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, 519 views)
Oct 29 '08 #1
4 7811
188 100+

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
Actually I fumbled with the project2.java 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 Project2.java file. I"m not sure if I can do that or am I suppose to call the the name file from Project2.java in the HouseGUI.java

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
13,262 8TB
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
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

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

Similar topics

by: Bob Lidgard | last post by:
Hi, I need some help. Hopefully this is trivial! I have created a special file type for my application. I can easily associate the app with the file type manually and I have even seen som code...
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
by: Max | last post by:
Yea this is probably a n00b question, but I haven't programmed C++ in at least 2 years and have never programmed for unix, sorry :) Anyway, I have a project in which a program is required to read...
by: mkarja | last post by:
Hi, I'm trying to figure out how to read some range of rows from a file. Is it possible to search the file with some criteria and then when the search string is found read 3 rows before and...
by: Robert | last post by:
I am creating a small help browser. It is pretty much finished except I cannot find how to pass a file to it (i.e. browser.exe index.html, or any html file, and the html file opens in the browser)...
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.