/** * This program illustrates reading data from a file into parallel * arrays for later processing. It does some unnecessary things, * and inefficiently too. (Can you identify those inefficiencies?) * * The program opens the input file, reads in the integer n, then * reads a list of n student records, where each record consists of * these four fields, separated by whitespace: * - first name * - last name * - email address * - which section they're in. * The program then prints out a list of email addresses of students * in Sections 3 and 4. * * To compile, type * kotlinc Students.kt * To run using [stduentsData.txt] as input file, type * kotlin StudentsKt studentsData.txt * and you should get the following output: * * Section 3 * --------- * swang * ataylor * rallen * rayyangar * ... * * Section 4 * --------- * drosner * dbarret * ecapra * pcombs * ... * */ fun main(args: Array) { val file = java.io.FileReader(args[0]) val scanner = java.util.Scanner(file) // read the number of students val n = scanner.nextInt() // initialize four parallel arrays val first = Array(n) { "" } val last = Array(n) { "" } val email = Array(n) { "" } val section = IntArray(n) // read in the data for (i in 0 until n) { first[i] = scanner.next() last[i] = scanner.next() email[i] = scanner.next() section[i] = scanner.nextInt() } // print email addresses of all students in section 3 println("""Section 3 |--------- """.trimMargin()) for (i in 0 until n) { if (section[i] == 3) println(email[i]) } println() // print email addresses of all students in section 4 println("""Section 4 |--------- """.trimMargin()) for (i in 0 until n) { if (section[i] == 4) { println(email[i]) } } scanner.close() file.close() }