INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Students.java.

/*************************************************************************
 *  Compilation:  javac Students.java
 *  Execution:    java Students < students.txt
 *  Dependencies: StdIn.java
 *
 *  Reads in the integer N from standard input, then a list
 *  of N student records, where each record consists of four
 *  fields, separated by whitespace:
 *      - first name
 *      - last name
 *      - email address
 *      - which section they're in
 *
 *  Then, print out a list of email address of students in section 4 
 *  and 5.
 *
 *************************************************************************/


public class Students {
    public static void main(String[] args) { 

        // read the number of students
        int N = StdIn.readInt();

        // initialize four parallel arrays
        String[] first = new String[N];
        String[] last  = new String[N];
        String[] email = new String[N];
        int[] section  = new int[N];

        // read in the data
        for (int i = 0; i < N; i++) {
            first[i]   = StdIn.readString();
            last[i]    = StdIn.readString();
            email[i]   = StdIn.readString();
            section[i] = StdIn.readInt();
        }

        // print email addresses of all students in section 4
        System.out.println("Section 4");
        System.out.println("---------");
        for (int i = 0; i < N; i++) {
            if (section[i] == 4) {
                System.out.println(email[i]);
            }
        }
        System.out.println();

        // print email addresses of all students in section 5
        System.out.println("Section 5");
        System.out.println("---------");
        for (int i = 0; i < N; i++) {
            if (section[i] == 5) {
                System.out.println(email[i]);
            }
        }

    }

}


Last updated: Mon Feb 23 09:37:13 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.