CS 1110/1111: Introduction to Programming

Lecture 15


Test 1 is in one week. LNEC students, contact LNEC today about taking the exam with them. We'll email the 50-minute-long test to them, expect it scanned and sent back by email.

University excused absence next Monday? Fill out this form.

Talking Points

There is a loop visualization tool.

continue skips the rest of the current pass through a loop; break ends the loop early (goes to whatever is after the loop).

for(int i = 0; i < 4; i += 1) {
    for(int j = 0; j < 4; j += 1) {
        for(int k = 0; k < 4; k += 1) {
            if (k == i) { 
                continue; // means "skip the rest of this pass through this loop" (the "k" loop)
            System.out.println("i = " + i +", j = " + j + ", k = " + k);
        if (j == i) { 
            break; // means "stop this loop now" (the "j" loop)

If you make a new PrintWriter("someFileName.txt") you can use it like you would System.out.

If you make a new File("someFileName.txt") you can use it like you would System.in.

Working with files, Java might loose its cool and freak out. Java calls this throwing an exception. It is possible to write code that catches the exception and calms Java back down, but we'll just say if Java freaks out our methods will freak out too.

public static void main(String[] args) throws Exception {
    // "throws Exception" on the line above means "main might freak out"
    File inFile = new File("remembrance.txt");
    if (inFile.exists()) {
        Scanner file = new Scanner(inFile); // one scanner per input source
        String remembered = file.nextLine();
        System.out.println("I remember \"" + remembered + "\"");

    Scanner keyboard = new Scanner(System.in); // one scanner per input source
    System.out.print("What is something worth remembering? ");
    String line = keyboard.nextLine();
    PrintWriter outFile = new PrintWriter("remembrance.txt");
    outFile.close(); // important! If you don't close it it might not save the file
Copyright © 2014 by Luther Tychonievich. All rights reserved.