Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: getActionCommand not working with arrayLists input from file?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default getActionCommand not working with arrayLists input from file?

    Hey people.

    I have this java file which makes a window, a bunch of arraylists, and some GUI things. It then fills the arrayLists with the data from a file I've linked to it, and then it looks for the action listeners (button being pressed). My arrayLists are: car type (car, suv, wagon), car brand (hyundai, VW etc.), car model(falcon, golf etc.), car price (duh), and car year(also duh). The idea is that each row in each array list refers to the same car - so value 1 of arrayList 1 contains information on the same car as value 1 of arrayList 2, and 3 etc.. OK so onto my problem. When I clear the search box, select a price, and select a car type (search box is now empty, should let through all cars), I get an error on line 220 - which is the getActionCommand line, comparing the selection in the GUI to the info in the arrayList. I might be being stupid here, this is the first time I've ever used radio buttons and the getActionCommand method so please tell me if I'm making a stupid mistake

    Oh a word about the CSV file, it has 5 columns, and each row is a new car - that's how the while loop works.

    Thanks guys, here's my code:
    Skeptile

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package usedcarapp;
     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Scanner;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.EventListener;
     
    /**
     *
     * @author will
     */
    public class UsedCarWindow extends JPanel {
     
        private Scanner fileScan;
     
        private JPanel leftPanel;
        private JPanel rightPanel;
     
        private JTextField searchBox;
     
        private JButton enterButton;
     
        private ButtonGroup carType;
        private ButtonGroup carPrice;
     
        private JRadioButton lower5k;
        private JRadioButton upto10k;
        private JRadioButton upto15k;
        private JRadioButton upto20k;
        private JRadioButton upto25k;
        private JRadioButton above25k;
     
     
        private JRadioButton car;
        private JRadioButton suv;
        private JRadioButton wagon;
        private JRadioButton none;
     
        private JComboBox priceMenu;
        //private String[] priceRanges = {"<$5k", "$5k - $10k", "$10k - $15k", "$15k - $20k", "$20k - $25k", ">$25k"};
     
        private ArrayList<String> carTypeList;
        private ArrayList<String> carYearList;
        private ArrayList<String> carBrandList;
        private ArrayList<String> carModelList;
        private ArrayList<String> carPriceList;
     
        public UsedCarWindow() {//this is where the main method starts
     
     
            File databaseFile = new File("cardata.csv");
     
            try {
                fileScan = new Scanner(databaseFile);
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
            }
     
            EventListener listener = new EventListener();
     
            //setting up the basic panels for the GUI
            leftPanel = new JPanel();
            rightPanel = new JPanel();
     
            leftPanel.setPreferredSize(new Dimension(200, 500));
            rightPanel.setPreferredSize(new Dimension(200, 500));
            setPreferredSize(new Dimension(1000,500));
     
            leftPanel.setBackground(Color.red);
            rightPanel.setBackground(Color.blue);
     
            //This is setting up the radio buttons for car type
            carType = new ButtonGroup();
     
            car = new JRadioButton("Car");
            car.setActionCommand("car");
            suv = new JRadioButton("SUV");
            suv.setActionCommand("suv");
            wagon = new JRadioButton("Wagon");
            wagon.setActionCommand("wagon");
            none = new JRadioButton("None");
            none.setActionCommand("none");
     
            carType.add(car);
            carType.add(suv);
            carType.add(wagon);
            carType.add(none);
     
            //Setting up the menu for the price range
     
            carPrice = new ButtonGroup();
     
            lower5k = new JRadioButton("Up to $5k ");
                lower5k.setActionCommand("5001");
     
            upto10k = new JRadioButton("Up to $10k");
                upto10k.setActionCommand("10001");
     
            upto15k = new JRadioButton("Up to $15k");
                upto15k.setActionCommand("15001");
     
            upto20k = new JRadioButton("Up to $20k");
                upto20k.setActionCommand("20001");
     
            upto25k = new JRadioButton("$Up to $25k");
                upto25k.setActionCommand("25001");
     
            above25k = new JRadioButton("Any Price                    ");
                above25k.setActionCommand("1000000000");
     
     
            carPrice.add(lower5k);
            carPrice.add(upto10k);
            carPrice.add(upto15k);
            carPrice.add(upto20k);
            carPrice.add(upto25k);
            carPrice.add(above25k);
     
     
     
            //Setting up the search box, and making it so that when you click it, it empties to allow you to type
            searchBox = new JTextField("Search here", 15);
     
            searchBox.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    searchBox.setText("");
                }
            });
     
            //Setting up the search button
            enterButton = new JButton("Enter");
     
            enterButton.addActionListener(listener);
     
            //Adding everything to the final GUI
            add(leftPanel);
           // add(rightPanel);
     
            leftPanel.add(searchBox);
            leftPanel.add(lower5k);
            leftPanel.add(upto10k);
            leftPanel.add(upto15k);
            leftPanel.add(upto20k);
            leftPanel.add(upto25k);
            leftPanel.add(above25k);
            leftPanel.add(car);
            leftPanel.add(suv);
            leftPanel.add(wagon);
            leftPanel.add(none);
            leftPanel.add(enterButton);
     
            //Makes an arrayList for each column in the CSV file
            carTypeList = new ArrayList<>();
            carYearList = new ArrayList<>();
            carBrandList = new ArrayList<>();
            carModelList = new ArrayList<>();
            carPriceList = new ArrayList<>();
     
     
            while (fileScan.hasNext()) {
                //this loop adds the data into the various arraylists to use later
     
                carTypeList.add(fileScan.next());
     
     
                carYearList.add(fileScan.next());
     
                carBrandList.add(fileScan.next());
     
                carModelList.add(fileScan.next());
     
                carPriceList.add(fileScan.next());
     
            }
     
        }
     
        private class EventListener implements ActionListener {
     
            @Override
            public void actionPerformed(ActionEvent e) {
     
                if (e.getSource() == enterButton){
     
                    int s = 0;
     
                    while(carTypeList.size()>=s){
     
     
                            if(carType.getSelection().getActionCommand().equals(carTypeList.get(s)) 
                            || carType.getSelection().getActionCommand().equals("none")){
     
                                if(Integer.parseInt(carPrice.getSelection().getActionCommand()) >= Integer.parseInt(carPriceList.get(s))){
     
                                    if(searchBox.getText().toLowerCase().contains(carBrandList.get(s)) 
                                    || searchBox.getText().toLowerCase().contains(carModelList.get(s))
                                    || searchBox.getText().toLowerCase().contains(carYearList.get(s) + "")
                                    ){
                                        //resultPanel resultPanel;
                                        //resultPanel = new resultPanel(carTypeList.get(s), carYearList.get(s), carBrandList.get(s), carModelList.get(s), carPriceList.get(s));
     
     
     
     
     
     
                                    }
                                }
                            }
     
                        }
     
     
                        s++;
                    }
                }
            }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: getActionCommand not working with arrayLists input from file?

    I get an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: getActionCommand not working with arrayLists input from file?

    Hey - sorry about that, completely slipped my mind. Here's the error, I'm using netbeans if that means anything. What I do is empty the search box, select a price, and select a car type and press enter, and this comes up:



    run:
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at usedcarapp.UsedCarWindow$EventListener.actionPerfo rmed(UsedCarWindow.java:220)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: getActionCommand not working with arrayLists input from file?

    Don't use parallel array/ArrayList designs for storing data unless you're forced to. Create a class, Car, and include all of those items you're currently storing in various ArrayLists as attributes or fields of the class.

    As for your error, it appears you're trying to read data outside the bounds of the collection.

Similar Threads

  1. JOptionPane Stops working after user input
    By dande in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 9th, 2014, 10:00 PM
  2. My text input file is not working correctly?
    By MLeclerc182 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2012, 08:34 PM
  3. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  4. Reading from a txt file into 3 arraylists and save it as double
    By depi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2011, 02:25 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM