Computer Science 210
Data Structures

Fall 2017, Siena College

CollegeInfo BlueJ Project

Click here to download a BlueJ project for CollegeInfo.


CollegeInfo Source Code

The Java source code for CollegeInfo is below. Click on a file name to download it.


CollegeInfo.java


/**
 * A program to read some information about local colleges
 *
 * @author Jim Teresco and the Fall 2017 CSIS 210 class at Siena
 */

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CollegeInfo
{
    public static void main(String args[]) throws IOException {

	// for now, we will assume the file has exactly these 22 entries
        final int NUM_COLLEGES = 22;
        Scanner infile = new Scanner(new File("localcolleges.txt"));
        int years[] = new int[NUM_COLLEGES];
        String names[] = new String[NUM_COLLEGES];      

	// read each line into our parallel arrays
        for (int i = 0; i < NUM_COLLEGES; i++) {
            years[i] = infile.nextInt();
	    // note: the substring is to remove the leading space
            names[i] = infile.nextLine().substring(1);
        }
        infile.close();
        
        System.out.println("Local college list: ");
        for (int i = 0; i < NUM_COLLEGES; i++) {
            System.out.println(names[i] + ", founded in " + years[i]);
        }
        
        // sequential search for oldest college and college with the
        // longest name
        int oldIndex = 0;
        int longIndex = 0;
        
        for(int i = 1; i < NUM_COLLEGES; i++) {
            
            if (years[i] < years[oldIndex]) {
                oldIndex = i;
            }
            
            if (names[i].length() > names[longIndex].length()) {
                longIndex = i;
            }
        }
        
        System.out.println("Oldest: " + names[oldIndex] + " (" + years[oldIndex] + ")");
        System.out.println("Longest name: " + names[longIndex]);
    }
}