Could anybody help me with this code
1. want to read a file into an array.
2.three button should sort it 1st product name should sort it alphabetically, 2nd maximum stock level which sort by maximum value, 3rd price sort by maximum value on the top.

thanx
cheers
Demon

Attachments
Netbook,10,320.90
External HDD",8,99
Laptop,5,699.70
LCD TV,5,699.60
Optical Mouse,4,25.99
Desktop PC,3,799.00
Desktop,3,729.00
USB Keyboard,2,19.50
Desk,2,250.90
Hub,20,26.50
Router,6,130.80
Antivirus,3,49.00
Window 7,9,210.00
public class ProductEntry
{
   public String ProdName;
   public int quantity;
   public Double price;


   public ProductEntry(String ProdName,int  quantity,Double price)
   {
	   this.ProdName = ProdName;
	   this.quantity=quantity;
	   this.price = price;
   }

   //accessor method
   public String ProdName()
   {
   		return ProdName;
   }

   public int getFirstName()
   {
       return quantity;
   }

	public Double getLastName()
	{
		return price;
	}


   	public String toString()
	{
		return ""+ ProdName+ "\t\t"+quantity + "\t\t" + price + "\n";
	}

}//end of class definition

Well here I'll suppose that your text file is in the same package as the other java files.
If it is so please made this change:

class LoadFromFileListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            try {
                InputStream u = getClass().getResourceAsStream("product.txt");
                BufferedReader  in = new BufferedReader(new InputStreamReader(u));
                String myEntry;
                while ((myEntry = in.readLine()) != null) {
                    StringTokenizer st = new StringTokenizer(myEntry, ",");
                    while (st.hasMoreTokens()) {
                        String ProdName = st.nextToken();
                        int quantity = Integer.parseInt(st.nextToken());
                        Double price = Double.parseDouble(st.nextToken());
                        myList.addLast(new ProductEntry(ProdName, quantity, price));

                        Display.setText("");
                        for (int i = 0; i < myList.size(); i++) {
                            Display.append(myList.get(i).toString());
                        }

                        Display.append("\n" + "TOTAL: " + myList.size() + "  Product(s) Found");
                    }
                }
                in.close();
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "File not found!", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
            }
        }
    }

Hope it helps.

This article has been dead for over six months. Start a new discussion instead.