473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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.
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
  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;
  10.  
  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;
  18.  
  19.    /**
  20.     * Creates this window
  21.     */
  22.    public HouseGUI()
  23.    {
  24.       // where will we input the args[0]
  25.       String inputFileName = args[0]; 
  26.  
  27.       setSize(400, 275);
  28.       setLocation(100, 100);
  29.       setTitle("The List of Houses and the Sorted List of Houses.");
  30.  
  31.       Container contentPane = getContentPane();
  32.  
  33.       JPanel panel = new JPanel();
  34.       panel.setLayout(new GridLayout(1, 2));
  35.       contentPane.add(panel, BorderLayout.CENTER);
  36.  
  37.       textAreaOriginal = new JTextArea();
  38.       textAreaOriginal.setEditable(false);
  39.       panel.add(new JScrollPane(textAreaOriginal));
  40.  
  41.       textAreaSorted = new JTextArea();
  42.       textAreaSorted.setEditable(false);
  43.       panel.add(new JScrollPane(textAreaSorted));
  44.  
  45.       messageField = new JTextField();
  46.       messageField.setEditable(false);
  47.       contentPane.add(messageField, BorderLayout.SOUTH);
  48.  
  49.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.       setVisible(true);
  51.  
  52.       int readFile(inputFileName,Home);
  53.  
  54.       displayNumbers(textAreaOriginal);
  55.  
  56.       displayNumbers(textAreaOriginal);
  57.    }  //constructor
  58.  
  59.    public static int readFile(String filename,House[] houses)
  60.    {
  61.        int lengthFilled = 0;
  62.        TextFileInput in = new TextFileInput(filename);
  63.  
  64.        // Read capacities into array:
  65.  
  66.        //reads the lines of txt file into the array
  67.  
  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
  80.  
  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
  91.  
  92.            // Release file for re-use:
  93.            in.close();
  94.  
  95.            return lengthFilled;       
  96.    }// readFile method
  97.  
  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
  107.  
  108.  
  109. }// HouseGUI 
Here's my Project2.java file

Expand|Select|Wrap|Line Numbers
  1. // Project2.java
  2.  
  3.  
  4.  
  5. public class Project2 
  6. {
  7.  
  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
  14.  
  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, 532 views)
Oct 29 '08 #1
4 7873
myusernotyours
188 100+
Hi,

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?

Regards,

Alex.
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
r035198x
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

3
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...
7
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 ...
2
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...
3
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...
11
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...
8
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)...
4
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...
8
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.