passing an array list

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 726 Views - Last Post: 21 April 2012 - 04:38 AM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

passing an array list

Posted 21 April 2012 - 01:50 AM

I am suppose to pass the populated arraylist/array to the PropertyFrame object. I don't know how to do that and I have looked in my book. If any one could help, thanks! here is my code

public static void main(String[] args) 
    {
       
        JFrame frame = new PropertyFrame(); 
        frame.setSize(400, 400); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
        
        /**
         * Check if the total number of arguments is exactly 1.
         */
        if (args.length < 1) {
            System.out.println("Usage: PropertyReader infile");
            return;
        }
        try {
            /**
             * Create an arraylist of Property Class.
             * This will hold objects of either Rental or Sale classes.
             */
            ArrayList<Property> propertyArrayList = new ArrayList<Property>();
            String inFileName = args[0];
            File inFile = new File(inFileName);
            /**
             * First Scanner object that reads one line at a time.
             */
            Scanner in = new Scanner(inFile);
            int lineCount = 0;
            /**
             * While loop which reads till end of file.
             */
            while (in.hasNextLine()) {
                lineCount++;
                String line = in.nextLine();
                /**
                 * Second Scanner object that splits the read line into
                 * chunks based on "," as the delimiter.
                 */
                Scanner lineScanner = new Scanner(line);
                lineScanner.useDelimiter("\\s*,\\s*");
                String propertyType = "";   //Variable that determines the type of the class
                /**
                 * Common variables for the property class
                 */
                int numOfBedrooms = 0;
                double numOfBathrooms = 0;
                double sqft = 0;
                String city = "";
                int zipcode = 0;
                // process a line and read data into variables.
                try {
                    propertyType = lineScanner.next();
                    numOfBedrooms = lineScanner.nextInt();
                    numOfBathrooms = lineScanner.nextDouble();
                    sqft = lineScanner.nextDouble();
                    city = lineScanner.next();
                    zipcode = lineScanner.nextInt();
                    /**
                     * Check if the property type is either Rental or Sale. If not then throw PropertyDataException
                     */
                    if (!((propertyType.equalsIgnoreCase("Rental"))
                            || (propertyType.equalsIgnoreCase("Buying")))) {
                        throw new PropertyDataException("Invalid Property Type on line " + line);
                    }
                    /**
                     * Check if the zip code is exactly 5 digits, else throw PropertyDataException
                     */
                    if (Integer.toString(zipcode).length() != 5) {
                        throw new PropertyDataException("Invalid zip code on line " + line);
                    }
                    /**
                     * If the property is Rental, read data specific for the Rental class
                     */
                    if (propertyType.equalsIgnoreCase("Rental")) {
                        double rent = lineScanner.nextDouble();
                        String management = lineScanner.next();
                        String petsboolean = lineScanner.next();
                        /**
                         * Check if pets are allowed or not, else throw PropertyDataException
                         */
                        if (petsboolean.equalsIgnoreCase("Y")) {
                            petsboolean = "Yes";
                        } else if (petsboolean.equalsIgnoreCase("N")) {
                            petsboolean = "No";
                        } else {
                            throw new PropertyDataException("Invalid Pets Allowed Boolean on line " + line);
                        }
                        /**
                         * Now the data is valid for Rental class, create the object for Rental class.
                         */
                        propertyArrayList.add(new Rental(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, rent, management, petsboolean));
                    } /**
                     * If the property is Sale, read data specific for the Sale class
                     */
                    else if (propertyType.equalsIgnoreCase("Buying")) {
                        double cost = lineScanner.nextDouble();
                        String seller = lineScanner.next();
                        /**
                         * Now the data is valid for Sale class, create the object for Sale class.
                         */
                        propertyArrayList.add(new Sale(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, cost, seller));
                    }
                    /**
                     * Output valid data to the command prompt.
                     */
                    System.out.println("Valid Data: " + line);
                } /**
                 * Catch InputMismatchException if data is in wrong sequence.
                 */
                catch (InputMismatchException exception) {
                    System.out.println("Invalid Data on line: " + line);
                } /**
                 * Catch NoSuchElementException if data is missing.
                 */
                catch (NoSuchElementException exception) {
                    System.out.println("Missing Data:" + line);
                } /**
                 * Catch PropertyDataException if data is wrong based on check criteria for properties.
                 */
                catch (PropertyDataException exception) {
                    System.out.println(exception.getMessage());
                }
            }  // end of while hasNextLine
            /**
             * Close the input file
             */
            in.close();
        } /**
         * Catch FileNotFoundException if input file not found.
         */
        catch (FileNotFoundException exception) {
            System.out.println("File " + args[0] + " Not Found!");
        }
        
     
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: passing an array list

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 02:03 AM

Objects are passed among class instances in at least 2 ways, through the class' constructor or using a setter (or mutator) of the class receiving the object. In your case, it appears an instance of the PropertyFrame class exists, therefore it's too late to use the constructor, so I'll assume the setter approach is best. To set the array in the PropertyFrame class from the class you've posted:

frame.setPropertyArray( propertyArrayList );

Then in your PropertyFrame class, the setter method signature will look something like;

protected void setPropertyArray( ArrayList propertyArrayList )
{
   this.propertyArrayList = propertyArrayList;
}

This is a general solution to a specific problem, so it may require tailoring to fit your PropertyFrame class and your purpose, since I can't know all of that.
Was This Post Helpful? 1
  • +
  • -

#3 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 02:14 AM

The setter method worked, but the


frame.setPropertyArray( propertyArrayList );

did not. It stated that cannot find symbol propertyArrayList.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 02:23 AM

I assume by it "worked," you mean that it compiled without error and not working means there was a compiler error.

It may be a problem with the visibility of the ArrayList propertyArrayList, since you declared it in your main method. If the ArrayList requires greater visibility to be seen by the statement you added, then declare the propertyArrayList as a class variable:

public ClassThatContainsMainMethod
{
   protected static ArrayList propertyArrayList;

   // etc . . . 
}

And remove the redundant declaration from the main() method.

If problems persist, post your updated code with errors.
Was This Post Helpful? 1
  • +
  • -

#5 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 03:10 AM

It gave me another error...
I don't understand why it is stating it cannot find symbol. the propertyArrayList there.
Here is my property reader:
public class PropertyReader 
{

    /**
     * Main method that accepts command-line arguments
     * @param args String array of command-line arguments
     */
    public static void main(String[] args) 
    {
       
        JFrame frame = new PropertyFrame(); 
        frame.setPropertyArray(propertyArrayList);
        frame.setSize(400, 400); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
        
        /**
         * Check if the total number of arguments is exactly 1.
         */
        if (args.length < 1) {
            System.out.println("Usage: PropertyReader infile");
            return;
        }
        try 
        {
            /**
             * Create an arraylist of Property Class.
             * This will hold objects of either Rental or Sale classes.
             */
            ArrayList<Property> propertyArrayList = new ArrayList<Property>();
            String inFileName = args[0];
            File inFile = new File(inFileName);
            /**
             * First Scanner object that reads one line at a time.
             */
            Scanner in = new Scanner(inFile);
            int lineCount = 0;
            /**
             * While loop which reads till end of file.
             */
            while (in.hasNextLine()) {
                lineCount++;
                String line = in.nextLine();
                /**
                 * Second Scanner object that splits the read line into
                 * chunks based on "," as the delimiter.
                 */
                Scanner lineScanner = new Scanner(line);
                lineScanner.useDelimiter("\\s*,\\s*");
                String propertyType = "";   //Variable that determines the type of the class
                /**
                 * Common variables for the property class
                 */
                int numOfBedrooms = 0;
                double numOfBathrooms = 0;
                double sqft = 0;
                String city = "";
                int zipcode = 0;
                // process a line and read data into variables.
                try {
                    propertyType = lineScanner.next();
                    numOfBedrooms = lineScanner.nextInt();
                    numOfBathrooms = lineScanner.nextDouble();
                    sqft = lineScanner.nextDouble();
                    city = lineScanner.next();
                    zipcode = lineScanner.nextInt();
                    /**
                     * Check if the property type is either Rental or Sale. If not then throw PropertyDataException
                     */
                    if (!((propertyType.equalsIgnoreCase("Rental"))
                            || (propertyType.equalsIgnoreCase("Buying")))) {
                        throw new PropertyDataException("Invalid Property Type on line " + line);
                    }
                    /**
                     * Check if the zip code is exactly 5 digits, else throw PropertyDataException
                     */
                    if (Integer.toString(zipcode).length() != 5) {
                        throw new PropertyDataException("Invalid zip code on line " + line);
                    }
                    /**
                     * If the property is Rental, read data specific for the Rental class
                     */
                    if (propertyType.equalsIgnoreCase("Rental")) {
                        double rent = lineScanner.nextDouble();
                        String management = lineScanner.next();
                        String petsboolean = lineScanner.next();
                        /**
                         * Check if pets are allowed or not, else throw PropertyDataException
                         */
                        if (petsboolean.equalsIgnoreCase("Y")) {
                            petsboolean = "Yes";
                        } else if (petsboolean.equalsIgnoreCase("N")) {
                            petsboolean = "No";
                        } else {
                            throw new PropertyDataException("Invalid Pets Allowed Boolean on line " + line);
                        }
                        /**
                         * Now the data is valid for Rental class, create the object for Rental class.
                         */
                        propertyArrayList.add(new Rental(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, rent, management, petsboolean));
                    } /**
                     * If the property is Sale, read data specific for the Sale class
                     */
                    else if (propertyType.equalsIgnoreCase("Buying")) {
                        double cost = lineScanner.nextDouble();
                        String seller = lineScanner.next();
                        /**
                         * Now the data is valid for Sale class, create the object for Sale class.
                         */
                        propertyArrayList.add(new Sale(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, cost, seller));
                    }
                    /**
                     * Output valid data to the command prompt.
                     */
                    System.out.println("Valid Data: " + line);
                } /**
                 * Catch InputMismatchException if data is in wrong sequence.
                 */
                catch (InputMismatchException exception) {
                    System.out.println("Invalid Data on line: " + line);
                } /**
                 * Catch NoSuchElementException if data is missing.
                 */
                catch (NoSuchElementException exception) {
                    System.out.println("Missing Data:" + line);
                } /**
                 * Catch PropertyDataException if data is wrong based on check criteria for properties.
                 */
                catch (PropertyDataException exception) {
                    System.out.println(exception.getMessage());
                }
            }  // end of while hasNextLine
            /**
             * Close the input file
             */
            in.close();
        } /**
         * Catch FileNotFoundException if input file not found.
         */
        catch (FileNotFoundException exception) {
            System.out.println("File " + args[0] + " Not Found!");
        }
        
     
    }
}


Here is my frame:
 
public class PropertyFrame extends JFrame
{
    private JButton searchbutton;
    private JRadioButton rentalradiobutton; 
    private JRadioButton saleradiobutton; 
    private ButtonGroup radiobuttongroup;
    private JTextField maxtextfield;
    private JComboBox bedroomscombobox; 
    private JComboBox bathroomscombobox; 
    private JLabel maxpricelabel; 
    private JLabel maxrentlabel; 
    private JLabel numberofbedroomslabel; 
    private JLabel numberofbathroomslabel; 
    private JPanel mainpanel; 
    private JPanel criteriapanel; 
    private JPanel searchpanel; 
    private JPanel resultspanel; 
    private JTextArea resultstextarea; 
    private ArrayList propertyArrayList; 
protected void setPropertyArray(ArrayList propertyArrayList)
    {
        this.propertyArrayList = propertyArrayList; 
    }
    
    public PropertyFrame()
     { 
        mainpanel = new JPanel();
        mainpanel.setLayout(new BorderLayout());
        criteriapanel = criteriaPanel();
        mainpanel.add(criteriapanel, BorderLayout.NORTH);
        searchpanel = searchPanel();
        mainpanel.add(searchpanel, BorderLayout.CENTER);
        resultspanel = resultsPanel(); 
        mainpanel.add(resultspanel, BorderLayout.SOUTH); 
        
        add(mainpanel); 
        
     }
    
    public JPanel criteriaPanel()
    {
        JPanel panel = new JPanel();
        
        maxpricelabel = new JLabel("Max Price:"); 
        maxrentlabel = new JLabel("Max Rent:");
        numberofbedroomslabel = new JLabel("Min # of Bedrooms:");
        numberofbathroomslabel = new JLabel("Min # of Bathrooms:"); 
        
        maxtextfield = new JTextField(); 
        
        class textfieldListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                int max = Integer.parseInt(maxtextfield.getText()); 
            }
        }
        ActionListener maxtextlistener = new textfieldListener();
        maxtextfield.addActionListener(maxtextlistener); 
        
        rentalradiobutton = new JRadioButton("Rental Property");
        saleradiobutton = new JRadioButton("Sale Property");
        
        class radiobuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if (rentalradiobutton.isSelected())
                        maxrentlabel.setVisible(true);
                else 
                        maxrentlabel.setVisible(false);                                               
                
                if (saleradiobutton.isSelected())
                    maxpricelabel.setVisible(true); 
                else
                    maxpricelabel.setVisible(false); 
            }
        }

        ActionListener listener = new radiobuttonListener();
        rentalradiobutton.addActionListener(listener);
        saleradiobutton.addActionListener(listener); 
        
        radiobuttongroup = new ButtonGroup();
        radiobuttongroup.add(rentalradiobutton);
        radiobuttongroup.add(saleradiobutton);
        
        bedroomscombobox = new JComboBox();
        bedroomscombobox.addItem(1); 
        bedroomscombobox.addItem(2); 
        bedroomscombobox.addItem(3); 
        bedroomscombobox.addItem(4); 
        bedroomscombobox.addItem(5); 
        
        bathroomscombobox = new JComboBox();
        bathroomscombobox.addItem(1);
        bathroomscombobox.addItem(2);
        bathroomscombobox.addItem(3);
        bathroomscombobox.addItem(4);
        bathroomscombobox.addItem(5);
        
        panel.setLayout(new GridLayout(4,1));
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Criteria:"));
        panel.add(rentalradiobutton);
        panel.add(saleradiobutton); 
        
        panel.add(maxpricelabel);
        maxpricelabel.setVisible(false);
        panel.add(maxrentlabel); 
        maxrentlabel.setVisible(false);
        panel.add(numberofbedroomslabel); 
        panel.add(numberofbathroomslabel); 
        panel.add(maxtextfield);  
        panel.add(bedroomscombobox); 
        bedroomscombobox.setEditable(true); 
        panel.add(bathroomscombobox); 
        bathroomscombobox.setEditable(true); 

        return panel; 
        
    }
    
  public JPanel searchPanel()
  {
      JPanel panel = new JPanel(); 
      
      searchbutton = new JButton("Search"); 
        
        class searchbuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
              int max = Integer.parseInt(maxtextfield.getText()); 
              String bed = (String)bedroomscombobox.getSelectedItem();
              String baths = (String)bathroomscombobox.getSelectedItem();
            }
        }

        ActionListener searchlistener = new searchbuttonListener();
        searchbutton.addActionListener(searchlistener);
        
        panel.setLayout(new FlowLayout());
        panel.add(searchbutton); 
        return panel; 
      
  }
    
    public JPanel resultsPanel()
    {
        JPanel panel = new JPanel();
        
        resultstextarea = new JTextArea(9,30);    
        JScrollPane scroll = new JScrollPane(resultstextarea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
        
        panel.setLayout(new FlowLayout()); 
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Results:"));
        panel.add(scroll);  
        resultstextarea.setEditable(false); 
        
        return panel; 
        
    }

}


This post has been edited by swim_5318: 21 April 2012 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 03:22 AM

I assume the error is complaining about your PropertyReader class? (Please post error messages.)

You've gotten the cart before the horse. You're using propertyArrayList at line 12 before it has been declared and initialized at line 30 and following. You didn't declare propertyArrayList as a class variable in PropertyReader as I'd suggested:
public class PropertyReader 
{
    // *** add this line (also change lines below) ***
    private static ArrayList<Property> propertyArrayList;
    /**
     * Main method that accepts command-line arguments
     * @param args String array of command-line arguments
     */
    public static void main(String[] args) 
    {
       
        JFrame frame = new PropertyFrame();

        // *** move this line to a point after it has been initialized *** // 
        frame.setPropertyArray(propertyArrayList);
        frame.setSize(400, 400); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 
        
        /**
         * Check if the total number of arguments is exactly 1.
         */
        if (args.length < 1) {
            System.out.println("Usage: PropertyReader infile");
            return;
        }
        try 
        {
            /**
             * Create an arraylist of Property Class.
             * This will hold objects of either Rental or Sale classes.
             */
            // *** change this line as shown ***
            propertyArrayList = new ArrayList<Property>();
            String inFileName = args[0];

etc . . . .

Was This Post Helpful? 1
  • +
  • -

#7 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 03:28 AM

I did just like you said. I moved my frame to the bottom. Here is my error message: cannot find symbol for setPropertyArray

Code:
public class PropertyReader 
{
    private static ArrayList<Property> propertyArrayList;


    /**
     * Main method that accepts command-line arguments
     * @param args String array of command-line arguments
     */
    public static void main(String[] args) 
    {
       
        
        
        /**
         * Check if the total number of arguments is exactly 1.
         */
        if (args.length < 1) {
            System.out.println("Usage: PropertyReader infile");
            return;
        }
        try 
        {
            /**
             * Create an arraylist of Property Class.
             * This will hold objects of either Rental or Sale classes.
             */
            ArrayList<Property> propertyArrayList = new ArrayList<Property>();
            String inFileName = args[0];
            File inFile = new File(inFileName);
            /**
             * First Scanner object that reads one line at a time.
             */
            Scanner in = new Scanner(inFile);
            int lineCount = 0;
            /**
             * While loop which reads till end of file.
             */
            while (in.hasNextLine()) {
                lineCount++;
                String line = in.nextLine();
                /**
                 * Second Scanner object that splits the read line into
                 * chunks based on "," as the delimiter.
                 */
                Scanner lineScanner = new Scanner(line);
                lineScanner.useDelimiter("\\s*,\\s*");
                String propertyType = "";   //Variable that determines the type of the class
                /**
                 * Common variables for the property class
                 */
                int numOfBedrooms = 0;
                double numOfBathrooms = 0;
                double sqft = 0;
                String city = "";
                int zipcode = 0;
                // process a line and read data into variables.
                try {
                    propertyType = lineScanner.next();
                    numOfBedrooms = lineScanner.nextInt();
                    numOfBathrooms = lineScanner.nextDouble();
                    sqft = lineScanner.nextDouble();
                    city = lineScanner.next();
                    zipcode = lineScanner.nextInt();
                    /**
                     * Check if the property type is either Rental or Sale. If not then throw PropertyDataException
                     */
                    if (!((propertyType.equalsIgnoreCase("Rental"))
                            || (propertyType.equalsIgnoreCase("Buying")))) {
                        throw new PropertyDataException("Invalid Property Type on line " + line);
                    }
                    /**
                     * Check if the zip code is exactly 5 digits, else throw PropertyDataException
                     */
                    if (Integer.toString(zipcode).length() != 5) {
                        throw new PropertyDataException("Invalid zip code on line " + line);
                    }
                    /**
                     * If the property is Rental, read data specific for the Rental class
                     */
                    if (propertyType.equalsIgnoreCase("Rental")) {
                        double rent = lineScanner.nextDouble();
                        String management = lineScanner.next();
                        String petsboolean = lineScanner.next();
                        /**
                         * Check if pets are allowed or not, else throw PropertyDataException
                         */
                        if (petsboolean.equalsIgnoreCase("Y")) {
                            petsboolean = "Yes";
                        } else if (petsboolean.equalsIgnoreCase("N")) {
                            petsboolean = "No";
                        } else {
                            throw new PropertyDataException("Invalid Pets Allowed Boolean on line " + line);
                        }
                        /**
                         * Now the data is valid for Rental class, create the object for Rental class.
                         */
                        propertyArrayList.add(new Rental(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, rent, management, petsboolean));
                    } /**
                     * If the property is Sale, read data specific for the Sale class
                     */
                    else if (propertyType.equalsIgnoreCase("Buying")) {
                        double cost = lineScanner.nextDouble();
                        String seller = lineScanner.next();
                        /**
                         * Now the data is valid for Sale class, create the object for Sale class.
                         */
                        propertyArrayList.add(new Sale(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, cost, seller));
                    }
                    /**
                     * Output valid data to the command prompt.
                     */
                    System.out.println("Valid Data: " + line);
                } /**
                 * Catch InputMismatchException if data is in wrong sequence.
                 */
                catch (InputMismatchException exception) {
                    System.out.println("Invalid Data on line: " + line);
                } /**
                 * Catch NoSuchElementException if data is missing.
                 */
                catch (NoSuchElementException exception) {
                    System.out.println("Missing Data:" + line);
                } /**
                 * Catch PropertyDataException if data is wrong based on check criteria for properties.
                 */
                catch (PropertyDataException exception) {
                    System.out.println(exception.getMessage());
                }
            }  // end of while hasNextLine
            /**
             * Close the input file
             */
            in.close();
        } /**
         * Catch FileNotFoundException if input file not found.
         */
        catch (FileNotFoundException exception) {
            System.out.println("File " + args[0] + " Not Found!");
        }
        
     JFrame frame = new PropertyFrame(); 
     frame.setPropertyArray(propertyArrayList);
     frame.setSize(400, 400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    }
}

This post has been edited by swim_5318: 21 April 2012 - 03:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 03:43 AM

I suspect your setPropertyArray() method signature doesn't match your method call due to my earlier shorthand. If propertyArrayList is of type ArrayList<Property>, then your variable declaration and the setPropertyArray() method signature should match:
private ArrayList<Property> propertyArrayList;

protected void setPropertyArray(ArrayList<Property> propertyArrayList)
{
    this.propertyArrayList = propertyArrayList; 
}

Have to keep things consistent. Make sense? Sorry if I added confusion.
Was This Post Helpful? 1
  • +
  • -

#9 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 03:51 AM

What you said makes sense, but I am still getting and error on the setPropertyArray. Line 145 in the first code is the error....

public class PropertyReader 
{
    private static ArrayList<Property> propertyArrayList;


    /**
     * Main method that accepts command-line arguments
     * @param args String array of command-line arguments
     */
    public static void main(String[] args) 
    {
       
        
        
        /**
         * Check if the total number of arguments is exactly 1.
         */
        if (args.length < 1) {
            System.out.println("Usage: PropertyReader infile");
            return;
        }
        try 
        {
            /**
             * Create an arraylist of Property Class.
             * This will hold objects of either Rental or Sale classes.
             */
            ArrayList<Property> propertyArrayList = new ArrayList<Property>();
            String inFileName = args[0];
            File inFile = new File(inFileName);
            /**
             * First Scanner object that reads one line at a time.
             */
            Scanner in = new Scanner(inFile);
            int lineCount = 0;
            /**
             * While loop which reads till end of file.
             */
            while (in.hasNextLine()) {
                lineCount++;
                String line = in.nextLine();
                /**
                 * Second Scanner object that splits the read line into
                 * chunks based on "," as the delimiter.
                 */
                Scanner lineScanner = new Scanner(line);
                lineScanner.useDelimiter("\\s*,\\s*");
                String propertyType = "";   //Variable that determines the type of the class
                /**
                 * Common variables for the property class
                 */
                int numOfBedrooms = 0;
                double numOfBathrooms = 0;
                double sqft = 0;
                String city = "";
                int zipcode = 0;
                // process a line and read data into variables.
                try {
                    propertyType = lineScanner.next();
                    numOfBedrooms = lineScanner.nextInt();
                    numOfBathrooms = lineScanner.nextDouble();
                    sqft = lineScanner.nextDouble();
                    city = lineScanner.next();
                    zipcode = lineScanner.nextInt();
                    /**
                     * Check if the property type is either Rental or Sale. If not then throw PropertyDataException
                     */
                    if (!((propertyType.equalsIgnoreCase("Rental"))
                            || (propertyType.equalsIgnoreCase("Buying")))) {
                        throw new PropertyDataException("Invalid Property Type on line " + line);
                    }
                    /**
                     * Check if the zip code is exactly 5 digits, else throw PropertyDataException
                     */
                    if (Integer.toString(zipcode).length() != 5) {
                        throw new PropertyDataException("Invalid zip code on line " + line);
                    }
                    /**
                     * If the property is Rental, read data specific for the Rental class
                     */
                    if (propertyType.equalsIgnoreCase("Rental")) {
                        double rent = lineScanner.nextDouble();
                        String management = lineScanner.next();
                        String petsboolean = lineScanner.next();
                        /**
                         * Check if pets are allowed or not, else throw PropertyDataException
                         */
                        if (petsboolean.equalsIgnoreCase("Y")) {
                            petsboolean = "Yes";
                        } else if (petsboolean.equalsIgnoreCase("N")) {
                            petsboolean = "No";
                        } else {
                            throw new PropertyDataException("Invalid Pets Allowed Boolean on line " + line);
                        }
                        /**
                         * Now the data is valid for Rental class, create the object for Rental class.
                         */
                        propertyArrayList.add(new Rental(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, rent, management, petsboolean));
                    } /**
                     * If the property is Sale, read data specific for the Sale class
                     */
                    else if (propertyType.equalsIgnoreCase("Buying")) {
                        double cost = lineScanner.nextDouble();
                        String seller = lineScanner.next();
                        /**
                         * Now the data is valid for Sale class, create the object for Sale class.
                         */
                        propertyArrayList.add(new Sale(numOfBedrooms, numOfBathrooms,
                                sqft, city, zipcode, cost, seller));
                    }
                    /**
                     * Output valid data to the command prompt.
                     */
                    System.out.println("Valid Data: " + line);
                } /**
                 * Catch InputMismatchException if data is in wrong sequence.
                 */
                catch (InputMismatchException exception) {
                    System.out.println("Invalid Data on line: " + line);
                } /**
                 * Catch NoSuchElementException if data is missing.
                 */
                catch (NoSuchElementException exception) {
                    System.out.println("Missing Data:" + line);
                } /**
                 * Catch PropertyDataException if data is wrong based on check criteria for properties.
                 */
                catch (PropertyDataException exception) {
                    System.out.println(exception.getMessage());
                }
            }  // end of while hasNextLine
            /**
             * Close the input file
             */
            in.close();
        } /**
         * Catch FileNotFoundException if input file not found.
         */
        catch (FileNotFoundException exception) {
            System.out.println("File " + args[0] + " Not Found!");
        }
        
     JFrame frame = new PropertyFrame(); 
     frame.setPropertyArray(propertyArrayList);
     frame.setSize(400, 400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    }
}


 private ArrayList<Property> propertyArrayList;

 
    protected void setPropertyArray(ArrayList<Property> propertyArrayList)

    {
        this.propertyArrayList = propertyArrayList; 
    }
    
    public PropertyFrame()
     { 
        mainpanel = new JPanel();
        mainpanel.setLayout(new BorderLayout());
        criteriapanel = criteriaPanel();
        mainpanel.add(criteriapanel, BorderLayout.NORTH);
        searchpanel = searchPanel();
        mainpanel.add(searchpanel, BorderLayout.CENTER);
        resultspanel = resultsPanel(); 
        mainpanel.add(resultspanel, BorderLayout.SOUTH); 
        
        add(mainpanel); 
        
     }
    
    public JPanel criteriaPanel()
    {
        JPanel panel = new JPanel();
        
        maxpricelabel = new JLabel("Max Price:"); 
        maxrentlabel = new JLabel("Max Rent:");
        numberofbedroomslabel = new JLabel("Min # of Bedrooms:");
        numberofbathroomslabel = new JLabel("Min # of Bathrooms:"); 
        
        maxtextfield = new JTextField(); 
        
        class textfieldListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                int max = Integer.parseInt(maxtextfield.getText()); 
            }
        }
        ActionListener maxtextlistener = new textfieldListener();
        maxtextfield.addActionListener(maxtextlistener); 
        
        rentalradiobutton = new JRadioButton("Rental Property");
        saleradiobutton = new JRadioButton("Sale Property");
        
        class radiobuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if (rentalradiobutton.isSelected())
                        maxrentlabel.setVisible(true);
                else 
                        maxrentlabel.setVisible(false);                                               
                
                if (saleradiobutton.isSelected())
                    maxpricelabel.setVisible(true); 
                else
                    maxpricelabel.setVisible(false); 
            }
        }

        ActionListener listener = new radiobuttonListener();
        rentalradiobutton.addActionListener(listener);
        saleradiobutton.addActionListener(listener); 
        
        radiobuttongroup = new ButtonGroup();
        radiobuttongroup.add(rentalradiobutton);
        radiobuttongroup.add(saleradiobutton);
        
        bedroomscombobox = new JComboBox();
        bedroomscombobox.addItem(1); 
        bedroomscombobox.addItem(2); 
        bedroomscombobox.addItem(3); 
        bedroomscombobox.addItem(4); 
        bedroomscombobox.addItem(5); 
        
        bathroomscombobox = new JComboBox();
        bathroomscombobox.addItem(1);
        bathroomscombobox.addItem(2);
        bathroomscombobox.addItem(3);
        bathroomscombobox.addItem(4);
        bathroomscombobox.addItem(5);
        
        panel.setLayout(new GridLayout(4,1));
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Criteria:"));
        panel.add(rentalradiobutton);
        panel.add(saleradiobutton); 
        
        panel.add(maxpricelabel);
        maxpricelabel.setVisible(false);
        panel.add(maxrentlabel); 
        maxrentlabel.setVisible(false);
        panel.add(numberofbedroomslabel); 
        panel.add(numberofbathroomslabel); 
        panel.add(maxtextfield);  
        panel.add(bedroomscombobox); 
        bedroomscombobox.setEditable(true); 
        panel.add(bathroomscombobox); 
        bathroomscombobox.setEditable(true); 

        return panel; 
        
    }
    
  public JPanel searchPanel()
  {
      JPanel panel = new JPanel(); 
      
      searchbutton = new JButton("Search"); 
        
        class searchbuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
              int max = Integer.parseInt(maxtextfield.getText()); 
              String bed = (String)bedroomscombobox.getSelectedItem();
              String baths = (String)bathroomscombobox.getSelectedItem();
            }
        }

        ActionListener searchlistener = new searchbuttonListener();
        searchbutton.addActionListener(searchlistener);
        
        panel.setLayout(new FlowLayout());
        panel.add(searchbutton); 
        return panel; 
      
  }
    
    public JPanel resultsPanel()
    {
        JPanel panel = new JPanel();
        
        resultstextarea = new JTextArea(9,30);    
        JScrollPane scroll = new JScrollPane(resultstextarea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
        
        panel.setLayout(new FlowLayout()); 
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Results:"));
        panel.add(scroll);  
        resultstextarea.setEditable(false); 
        
        return panel; 
        
    }

}




Thanks for all of your help by the way.
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 03:59 AM

Okay. We'll get there. Post your Property class so that I can put it all together and figure out what's wrong.
Was This Post Helpful? 1
  • +
  • -

#11 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 04:01 AM

Property class
public abstract class Property implements Comparable 
{
    /**
     * Instance fields specific to the Property which are common for rental as well as
     * sale property.
     */
    private int numOfBedrooms;
    private double numOfBathrooms;
    private double sqft;
    private String city;
    private int zipcode;

    /**
     * @return the numOfBedrooms
     */
    public int getNumOfBedrooms() {
        return numOfBedrooms;
    }

    /**
     * @param bedrooms the numOfBedrooms to set
     */
    public void setNumOfBedrooms(int bedrooms) {
        this.numOfBedrooms = bedrooms;
    }

    /**
     * @return the numOfBathrooms
     */
    public double getNumOfBathrooms() {
        return numOfBathrooms;
    }

    /**
     * @param bathrooms the numOfBathrooms to set
     */
    public void setNumOfBathrooms(double bathrooms) {
        this.numOfBathrooms = bathrooms;
    }

    /**
     * @return the sqft
     */
    public double getSqft() {
        return sqft;
    }

    /**
     * @param area the sqft to set
     */
    public void setSqft(double area) {
        this.sqft = area;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param livingcity the city to set
     */
    public void setCity(String livingcity) {
        this.city = livingcity;
    }

    /**
     * @return the zipcode
     */
    public int getZipcode() {
        return zipcode;
    }

    /**
     * @param areacode the zipcode to set
     */
    public void setZipcode(int areacode) {
        this.zipcode = areacode;
    }

    /**
     *
     * @return the string representation of the values of the attributes for the
     * Property class.
     */
    public String toString()
    {
        return getClass().getName() + " [Bedrooms: " + numOfBedrooms + "][Bathrooms: " + numOfBathrooms
                + "][SQFT: " + sqft + "][City: " + city + "][Zipcode: " + zipcode + "]";
    }

    /**
     * Constructor for the Property class that accepts 5 arguments
     * @param bedrooms
     * @param bathrooms
     * @param area
     * @param livingcity
     * @param areacode
     */
    public Property(int bedrooms, double bathrooms, double area, String livingcity, int areacode)
    {
        numOfBedrooms = bedrooms;
        numOfBathrooms = bathrooms;
        sqft =  area;
        city = livingcity;
        zipcode = areacode;
    }
    
    public int compareTo(Object otherObject)
    {
        
        int compareSqft = (int) ((Property) otherObject).getSqft(); 
 
		//ascending order
		return (int) (this.sqft - compareSqft);
 
    }
}

Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 04:06 AM

Darnit! I hate it when the obvious is so hard to see.

Instead of

JFrame frame = new PropertyFrame();

use

PropertyFrame frame = new PropertyFrame();

That should clear up everything we've been talking about. Now you have to actually use the propertyArrayList in your PropertyFrame.
Was This Post Helpful? 1
  • +
  • -

#13 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 04:13 AM

It made my error go away but now my GUI is not showing up when I run...

Never mind. I fixed it. Thank you so much for your help!
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: passing an array list

Posted 21 April 2012 - 04:14 AM

Glad to hear and happy to help.
Was This Post Helpful? 0
  • +
  • -

#15 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: passing an array list

Posted 21 April 2012 - 04:31 AM

Could you help me to get the array to sort now? I tried, but am getting errors: method is not applicable

PropertyFrame code:
public class PropertyFrame extends JFrame
{
    private JButton searchbutton;
    private JRadioButton rentalradiobutton; 
    private JRadioButton saleradiobutton; 
    private ButtonGroup radiobuttongroup;
    private JTextField maxtextfield;
    private JComboBox bedroomscombobox; 
    private JComboBox bathroomscombobox; 
    private JLabel maxpricelabel; 
    private JLabel maxrentlabel; 
    private JLabel numberofbedroomslabel; 
    private JLabel numberofbathroomslabel; 
    private JPanel mainpanel; 
    private JPanel criteriapanel; 
    private JPanel searchpanel; 
    private JPanel resultspanel; 
    private JTextArea resultstextarea; 
    private ArrayList<Property> propertyArrayList;

 
    protected void setPropertyArray(ArrayList<Property> propertyArrayList)

    {
        this.propertyArrayList = propertyArrayList; 
        Arrays.sort(propertyArrayList); 
    }
    
    public PropertyFrame()
     { 
        mainpanel = new JPanel();
        mainpanel.setLayout(new BorderLayout());
        criteriapanel = criteriaPanel();
        mainpanel.add(criteriapanel, BorderLayout.NORTH);
        searchpanel = searchPanel();
        mainpanel.add(searchpanel, BorderLayout.CENTER);
        resultspanel = resultsPanel(); 
        mainpanel.add(resultspanel, BorderLayout.SOUTH); 
        
        add(mainpanel); 
        
     }
    
    public JPanel criteriaPanel()
    {
        JPanel panel = new JPanel();
        
        maxpricelabel = new JLabel("Max Price:"); 
        maxrentlabel = new JLabel("Max Rent:");
        numberofbedroomslabel = new JLabel("Min # of Bedrooms:");
        numberofbathroomslabel = new JLabel("Min # of Bathrooms:"); 
        
        maxtextfield = new JTextField(); 
        
        class textfieldListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                int max = Integer.parseInt(maxtextfield.getText()); 
            }
        }
        ActionListener maxtextlistener = new textfieldListener();
        maxtextfield.addActionListener(maxtextlistener); 
        
        rentalradiobutton = new JRadioButton("Rental Property");
        saleradiobutton = new JRadioButton("Sale Property");
        
        class radiobuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if (rentalradiobutton.isSelected())
                        maxrentlabel.setVisible(true);
                else 
                        maxrentlabel.setVisible(false);                                               
                
                if (saleradiobutton.isSelected())
                    maxpricelabel.setVisible(true); 
                else
                    maxpricelabel.setVisible(false); 
            }
        }

        ActionListener listener = new radiobuttonListener();
        rentalradiobutton.addActionListener(listener);
        saleradiobutton.addActionListener(listener); 
        
        radiobuttongroup = new ButtonGroup();
        radiobuttongroup.add(rentalradiobutton);
        radiobuttongroup.add(saleradiobutton);
        
        bedroomscombobox = new JComboBox();
        bedroomscombobox.addItem(1); 
        bedroomscombobox.addItem(2); 
        bedroomscombobox.addItem(3); 
        bedroomscombobox.addItem(4); 
        bedroomscombobox.addItem(5); 
        
        bathroomscombobox = new JComboBox();
        bathroomscombobox.addItem(1);
        bathroomscombobox.addItem(2);
        bathroomscombobox.addItem(3);
        bathroomscombobox.addItem(4);
        bathroomscombobox.addItem(5);
        
        panel.setLayout(new GridLayout(4,1));
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Criteria:"));
        panel.add(rentalradiobutton);
        panel.add(saleradiobutton); 
        
        panel.add(maxpricelabel);
        maxpricelabel.setVisible(false);
        panel.add(maxrentlabel); 
        maxrentlabel.setVisible(false);
        panel.add(numberofbedroomslabel); 
        panel.add(numberofbathroomslabel); 
        panel.add(maxtextfield);  
        panel.add(bedroomscombobox); 
        bedroomscombobox.setEditable(true); 
        panel.add(bathroomscombobox); 
        bathroomscombobox.setEditable(true); 

        return panel; 
        
    }
    
  public JPanel searchPanel()
  {
      JPanel panel = new JPanel(); 
      
      searchbutton = new JButton("Search"); 
        
        class searchbuttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
              int max = Integer.parseInt(maxtextfield.getText()); 
              String bed = (String)bedroomscombobox.getSelectedItem();
              String baths = (String)bathroomscombobox.getSelectedItem();
            }
        }

        ActionListener searchlistener = new searchbuttonListener();
        searchbutton.addActionListener(searchlistener);
        
        panel.setLayout(new FlowLayout());
        panel.add(searchbutton); 
        return panel; 
      
  }
    
    public JPanel resultsPanel()
    {
        JPanel panel = new JPanel();
        
        resultstextarea = new JTextArea(9,30);    
        JScrollPane scroll = new JScrollPane(resultstextarea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
        
        panel.setLayout(new FlowLayout()); 
        panel.setBorder(new TitledBorder(new EtchedBorder(),"Search Results:"));
        panel.add(scroll);  
        resultstextarea.setEditable(false); 
        
        return panel; 
        
    }

}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2