// array to hold students // will hold at least 20 students Student [] students = new Student[20]; // number of students actually stored in array (between 0 and 20) int numstudents = 0; // read student names from file and save in 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 student name, gender, age and height String name = "", gender = ""; int age = 0, height = 0; try { // open the file and set up the scanner for reading CSV in = new BufferedReader(new FileReader("students.txt")); 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()) { name = sc.next(); //System.out.println("item 1 " + name); } if (sc.hasNext()) { gender = sc.next(); //System.out.println("item 2 " + gender); } if (sc.hasNextInt()) { age = sc.nextInt(); } if (sc.hasNextInt()) { height = sc.nextInt(); } // add these items to a new Student object Student oneStudent = new Student(name, gender, age, height); // add this student data to the array students[count] = oneStudent; count++; } // save the number of items read from the file numstudents = 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"); } // display all students private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String message = "Students:

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

" + oneStudent.toString(); } // display the result jLabel2.setText(message + ""); } // search for students whose name contains the search string private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // string to search for in the student names String searchname; searchname = jTextField1.getText(); // variables to format results String resultheader = "Result:

"; String resultnames = ""; // search array for all matches String oneName; for (int i = 0; i < numstudents; i++) { //compare search name anywhere in student name, ignore case oneName = students[i].getStudentName(); if (oneName.toLowerCase().indexOf(searchname.toLowerCase()) != -1) { resultnames = resultnames +"

" + students[i].toString(); } } // end of for loop // if resultnames is still empty, then no matches were found if(resultnames.equals("")) { resultnames = "No matches found"; } // display result in a multi-line label String result = resultheader + resultnames; jLabel5.setText(result); } // use bubble sort to sort the array of students based on height private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Student tempStudent; // the bubble sort // each iteration of the outer loop is a "pass" through an interval of the array // where the number of passes is one less than the size of the array for (int i = 0; i < (numstudents - 1); i++ ) { // the inner loop iterates over elements of the array in the interval, // which decreases in size by 1 each pass for (int j = 0; j < (numstudents - (i+1)); j++) { // for each student in the array, compare the height fields // if the height of the first student is greater, swap the students if (students[j].getHeight() > students[j+1].getHeight()) { tempStudent = students[j]; students[j] = students[j+1]; students[j+1] = tempStudent; } } } // end of sorting // display all students in sorted order in a multiline label String outputtext = "Students sorted by height:

"; // for each student in the (now sorted) array, add that student to the list for (int i = 0; i < numstudents; i++) { outputtext = outputtext + "

" + students[i].toString(); } // display the students using the same label as before jLabel2.setText(outputtext); }