Previously on C212:

https://www.cs.indiana.edu/classes/c212-dgerman/spr2015/exam001.jpg

https://www.cs.indiana.edu/classes/c212-dgerman/spr2015/exam001.jpg

From the command line interface: 

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\System32>c:

C:\Windows\System32>cd \

C:\>cd Users

C:\Users>cd dgerman

C:\Users\dgerman>cd Desktop

C:\Users\dgerman\Desktop>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 6014-65BE

 Directory of C:\Users\dgerman\Desktop

07/01/2015  11:55 AM    <DIR>          .
07/01/2015  11:55 AM    <DIR>          ..
06/29/2015  12:00 PM        12,928,003 drjava-stable-20140826-r5761.jar
07/01/2015  11:51 AM    <DIR>          jun 30
07/01/2015  11:55 AM               355 Mark.class
07/01/2015  11:55 AM                77 Mark.java
07/01/2015  11:55 AM                27 Mark.java~
07/01/2015  11:53 AM               104 thefly.txt
               5 File(s)     12,928,566 bytes
               3 Dir(s)  55,383,379,968 bytes free

C:\Users\dgerman\Desktop>type thefly.txt
The Fly
by Ogden Nash

The Lord in His wisdom made the fly,
And then forgot to tell us why.


C:\Users\dgerman\Desktop>

Now I write and explain this program:

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

public class Mark {
  public static void main(String[] args) throws Exception {
    Scanner speckles = new Scanner( new File( "thefly.txt" ) ); 
    while (speckles.hasNext()) {
      String token = speckles.next(); 
      System.out.println( "***(" + token + ")***" ); 
    }
  }
}

This produces the following output: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
***(The)***
***(Fly)***
***(by)***
***(Ogden)***
***(Nash)***
***(The)***
***(Lord)***
***(in)***
***(His)***
***(wisdom)***
***(made)***
***(the)***
***(fly,)***
***(And)***
***(then)***
***(forgot)***
***(to)***
***(tell)***
***(us)***
***(why.)***


In lab you will have to write a program that reads a file like The Fly and

(a) reports the number of lines

(b) reports the number of words

(c) reports the number of characters: non-white-space, and total. 

(d) report the average word length (in characters) 

(e) report the average line length (in words or tokens) 

Now let's discuss Exceptions. 

public class Mark {
  public static void main(String[] args) {
    String number = "12"; 
    int n = Integer.parseInt( number ); 
    System.out.println( 1 + n ); 
  }
}

Nothing unusual here except is happening. 

public class Mark {
  public static void main(String[] args) {
    String number = " 12"; 
    int n = Integer.parseInt( number ); 
    System.out.println( 1 + n ); 
  }
}

A small change leads to this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
java.lang.NumberFormatException: For input string: " 12"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Mark.main(Mark.java:4)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)


Now I will write a program that asks for an integer: 

import java.util.Scanner; 

public class Mark {
  public static void main(String[] args) {
    Scanner speckles = new Scanner(System.in); 
    System.out.print("integer> "); 
    String line = speckles.nextLine(); 
    int number = Integer.parseInt( line ); 
    System.out.println( Math.sqrt( number ) ); 
    
  }
}

Here's how this runs: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
integer>  [DrJava Input Box]
3.1622776601683795


Here's another situation: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
integer>  [DrJava Input Box]
java.lang.NumberFormatException: For input string: "12w"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Mark.main(Mark.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)


Here's a change: 

import java.util.Scanner; 

public class Mark {
  public static void main(String[] args) {
    Scanner speckles = new Scanner(System.in); 
    System.out.print("integer> "); 
    String line = speckles.nextLine(); 
try {
    int number = Integer.parseInt( line ); 
    System.out.println( Math.sqrt( number ) ); 
} catch (Exception e) {
    // System.out.println( e );  
    System.out.println( "That's not an integer."); 
}
    
  }
}

Here's how the program works now: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
integer> 12w
That's not an integer.


Attendance: 
 Mark , Logan , Daniel , Jared   , Austin  , Trevor
 Judy , Aleksa, Walter , Adam    , Gabriela, James , Brennan , Jacquelyn
 Nick , Yiming, Jingzhe, Zac     , Paul    , Morgan, Alex Ong, William Bobe 
 Jon  , Grant , Michael Alexander, Mohan   , Jack  , Qin     , Lauren

public class Mark {
  public static void main(String[] args) {
    Scanner speckles = new Scanner(System.in); 
    while (true) {
      System.out.print("integer> "); 
      String line = speckles.nextLine(); 
      try {
        int number = Integer.parseInt( line ); 
        System.out.println( Math.sqrt( number ) ); 
        break; 
      } catch (Exception e) {
        // System.out.println( e );  
        System.out.println( "That's not an integer."); 
      }
    }      
  }
}

This is how this program works now: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
That's not an integer.
integer>  [DrJava Input Box]
3.4641016151377544


http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)

Now we discuss the basics of hash maps (or maps, or hash tables):

import java.util.*; 

public class Mark {
  public static void main(String[] args) {
    Map<String, String> preferences;
    preferences = new HashMap<String, String>();
    preferences.put("marlshep", "Pistons"); 
    preferences.put("jonman", "Celtics"); 
    preferences.put("alexongw", "Spurs"); 
    preferences.put("marlshep", "Pacers");     
    System.out.println( preferences ); 
  }
}

This runs and produces:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mark
{jonman=Celtics, marlshep=Pacers, alexongw=Spurs}


See you in lab!

--