// array to hold candy sales data // will hold at least 20 candy sales CandySales [] sales = new CandySales[20]; // number of candy sales actually stored in array (between 0 and 20) int numsales = 0; // read the data from the file specified by the user and store in the array private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: BufferedReader in = null; Scanner sc = null; // count the number of candy sales read from the file int count = 0; // read file: each line has a person name, candy name and sales number String person = "", candy = ""; int numcandy = 0; try { // get the name of the file typed by the user String filename = jTextField1.getText(); // open the file and set up the scanner for reading CSV in = new BufferedReader(new FileReader(filename)); sc = new Scanner(in); // use comma as a delimiter sc.useDelimiter(",|(\\n|\\r)+"); // each loop iteration will read one line of the file // each new non-empty line will begin with a string while(sc.hasNext()) { // read the three items from this line if (sc.hasNext()) { person = sc.next(); //System.out.println("item 1 " + person); } if (sc.hasNext()) { candy = sc.next(); //System.out.println("item 2 " + candy); } if (sc.hasNextInt()) { numcandy = sc.nextInt(); //System.out.println("item 3 " + numcandy); } // add these items to a new CandySales object CandySales oneSale = new CandySales(person, candy, numcandy); // add this sale data to the array sales[count] = oneSale; count++; } // save the number of items read from the file numsales = count; // after all lines are read, close the file sc.close(); } catch (Exception e) { System.out.println(e.toString()); System.exit(-1); } // display the file status message jLabel1.setText("Done Reading File"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // initialize string for multiline form label String message = "Candy Sales:

"; // variable for each candy sale item CandySales oneSale; // loop over all defined items in the sales array for (int i = 0; i < numsales; i++) { oneSale = sales[i]; message = message + "

" + oneSale.toString(); } // display the result jLabel2.setText(message); }