-4

okay look i know this question has been answered time and time again HOWEVER i seriously can't figure out what im doing wrong.

please help.

i have two classes. Machine.java which is the object class and the InventoryTest.java class that holds the main statement.

everywhere i have read says this should work.

this is in the Machine.java class

public static Comparator<Machine> machineIdNumberComparator = new Comparator<Machine>() {

    @Override
public int compare(Machine s1, Machine s2) {
   String MachineID1 = s1.getMachineIdNumber();
   String MachineID2 = s2.getMachineIdNumber();

   //ascending order
   return MachineID1.compareTo(MachineID2);
}};

this is in the InventoryTest

        Collections.sort(arraylist, inventoryArray.machineIdNumberComparator);

this line comes up with problems. arraylist has cannot find symbol error and so does the machineIdNumberComparator.

i need to be able to sort by 3 different methods.

edit: this is how i add and created my arrayList

public List<Machine> inventoryArray = new ArrayList<>();

inventoryArray.add(new Machine(strMachineIdNumber, strManufacturer, strType, dblPowerOrCapacity, dblPrice));

the arraylist is declaired at the start just under the public class where you declair all your variables.

/**
*
* @author stephankranenfeld
*/
import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextArea;

public class InventoryTest extends JFrame {

private static final int FRAME_WIDTH = 800;
private static final int FRAME_HEIGHT = 1500;
private final JTextArea textArea;
private final JButton addMachine;
private final JButton removeMachine;
private final JButton exitButton;
private final JButton sortButton;
final String START_TEXT = String.format("%-20s %-20s %-10s %15s %10s %n", "Machine ID Number:", "Manufacturer:", "Type:", "Power/Capacity:", "Price:"); // starting line of texted used in both display all and entre data buttons.
final String DEVIDER = "-------------------------------------------------------------------------------------\n";// a devider used multple times
public List<Machine> inventoryArray = new ArrayList<>();

 /*
junk that isn't needed i think
*/

private void sort() {
    JComboBox sortBy = new JComboBox();
    sortBy.addItem("ID Number");
    sortBy.addItem("Manufacturer");
    sortBy.addItem("Type");

    int option = JOptionPane.showConfirmDialog(null, sortBy, "Sort by?", JOptionPane.OK_CANCEL_OPTION);
    if (option == JOptionPane.OK_OPTION) 
    {
        //if id is selected sort by id
        //if manufacturer is selected sor by manufacturer
        //if type is selected sort by type
        Collections.sort(inventoryArray, Machine.machineIdNumberComparator);
    }
}
}


 public class Machine{
/*
* all the get and set methods
*/
    public static Comparator<Machine> machineIdNumberComparator = new Comparator<Machine>() {

    @Override
public int compare(Machine s1, Machine s2) {
   String MachineID1 = s1.getMachineIdNumber();
   String MachineID2 = s2.getMachineIdNumber();

   //ascending order
   return MachineID1.compareTo(MachineID2);
}};
Daedric
  • 1
  • 6
  • 3
    Please show a short but *complete* program demonstrating the problem, and give the *complete* error message. Not just "arraylist has cannot find symbol error and so does the machineIdNumberComparator" - which symbol? And why you trying to refer to the comparator as `inventoryArray.machineIdNumberComparator` when it's in a class which I *hope* is called `Machine`? (I'd expect `Machine.machineIdNumberComparator`.) I strongly suspect this problem has nothing to do with sorting, and everything to do with you just not referring to variables properly. – Jon Skeet Aug 28 '14 at 06:28
  • Can you please attach all the source code. – Siva Kumar Aug 28 '14 at 06:30
  • anyway thanks guys. as Jon Skeet said i had it wrong and now that is changed it works @JonSkeet thanks for that! – Daedric Aug 28 '14 at 06:45

1 Answers1

0

The second argument of Collections.sort() should be Machine.machineIdNumberComparator

SparkOn
  • 8,806
  • 4
  • 29
  • 34
dpassy
  • 363
  • 1
  • 10