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++; } } } }