teaching machines

CS 145 Lecture 6 – Scanner and String

September 16, 2015 by . Filed under cs145, fall 2015, lectures.

Agenda

TODO

Note

We’ve been looking at methods of the Math class and thinking of them as blackboxes that accept our input, compute some value that we want without us needing to know how it does it, and give the value back to us. In fact, our entire programs can be thought of in the same way, as machines that turn input into output. Unfortunately, our input so far has had to be typed directly into our code. We fix that today by introducing the Scanner class, which helps us read what the user types at the keyboard.

Along the way we’ll round out our discussion of the eight Java primitives by looking at boolean and char. When we bundle these primitives together into more complex types, we get objects. Scanner and String are the first two objects we’ll consider in detail. We’ll start to focus a lot more on text processing.

The pattern for creating primitives and objects is similar, except the types are classes (with a capital letter), and we construct them by invoking a constructor:

type id = expr; // creating a primitive
Type id = new Type(...); // creating an object

To help demonstrate the use of Scanner and String, we’ll tackle some of these problems:

  1. How tall is the user metrically?
  2. MADLIB
  3. You ask the user for a credit card number. Print it back to the console with all but the last four digits as asterisks.
  4. Students and teachers get a discount at your website. How can you tell if someone’s a student or teacher?
  5. You ask 112 people for their UWEC username—and username only. Many of them append @uwec.edu anyway. How can you clean up their input?
  6. Your camera takes pictures that are X pixels wide by Y pixels tall. How many megapixels is that?

Code

LastOfThePrimitives.java

package lecture0916;

public class LastOfThePrimitives {
  public static void main(String[] args) {
    boolean isOn = true;
    boolean isOff = false;
    
    char c = 'a';
    c = (char) (c + 1);
    System.out.println(c);
    
    char z = '\u2124';
    System.out.println(z);
    
    System.out.println("\u265b\u2603\u2672\u2697\u266A\u2682");
  }
}

Metricater.java

package lecture0916;

import java.util.Scanner;

public class Metricater {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.println("\uD83C\uDF66 Enter your height in feet and inches: ");
    
    int feet = in.nextInt();
    int inches = in.nextInt();
    
    inches = inches + feet * 12;
    
    double centimeters = 2.54 * inches;
    
    System.out.println("Your height in cm: " + centimeters);
  }
}

MadLib.java

package lecture0916;

import java.util.Scanner;

public class MadLib {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Verb: ");
    String verb1 = in.nextLine();
    System.out.print("Noun: ");
    String noun1 = in.nextLine();
    System.out.print("Noun: ");
    String noun2 = in.nextLine();
    System.out.print("Noun: ");
    String noun3 = in.nextLine();
    System.out.print("Place: ");
    String place1 = in.nextLine();
    System.out.print("Adjective: ");
    String adjective1 = in.nextLine();
    System.out.print("Integer: ");
    int number1 = in.nextInt();
    in.nextLine();
    System.out.print("Verb: ");
    String verb2 = in.nextLine();
    System.out.print("Noun: ");
    String noun4 = in.nextLine();
    
    String template = "Do you " + verb1 + " for the " + noun1 + "?\n\nWe are interested in the qualities and " + noun2 + " of " + noun1 + " supporters, as well as their emotional reactions to " + noun3 + ".\n\nThis study is completed online in several short surveys you can complete from " + place1 + " during the " + adjective1 + " season. You must be at least " + number1 + " years old to " + verb2 + ". If you complete all of this online study, you will be entered into a lottery drawing for a " + noun4 + ".";
    System.out.println(template);
  }
  
  private static final String original = "Do you root for the Green Bay Packers?\n\nWe are interested in the qualities and behaviors of Green Bay Packers supporters, as well as their emotional reactions to games.\n\nThis study is completed online in several short surveys you can complete from home during the football season. You must be at least 18 years old to participate. If you complete all of this online study, you will be entered into a lottery drawing for a $100 prize.";
  private static final String template = "Do you VERB1 for the NOUN1?\n\nWe are interested in the qualities and NOUN2 of NOUN1 supporters, as well as their emotional reactions to NOUN3.\n\nThis study is completed online in several short surveys you can complete from PLACE1 during the ADJECTIVE1 season. You must be at least NUMBER1 years old to VERB3. If you complete all of this online study, you will be entered into a lottery drawing for a NOUN4.";
}

Haiku

Numbers don’t change us
Our hearts are wrapped up in chains
Chains of words: stories