Building Java Programs, 3rd edition

Lab: Scanner

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Lab goals

Goals for this problem set:

User input and Scanner

Method name Description
nextInt() reads and returns the next token as an int, if possible
nextDouble() reads and returns the next token as double, if possible
next() reads and returns a single word as a String
nextLine() reads and returns an entire line as a String

Example:

import java.util.*;   // so you can use Scanner
...
Scanner console = new Scanner(System.in);
System.out.print("How old are you? ");   // prompt
int age = console.nextInt();
System.out.println("You typed " + age);

Exercise : Syntax errors

Exercise - answer

  1. line 5: nextString should be next
  2. line 9: string should be String
  3. line 9: name should not be in quotes
  4. line 10: Whitaker should be in quotes
  5. line 10: cannot compare strings with ==; must use .equals
  6. line 13: cannot call replace without specifying a string object (name)
  7. line 14: toUppercase should be toUpperCase
  8. line 14: name. should come before toUpperCase, not passed as a parameter to it
  9. line 14: must say name = to store the result of toUpperCase
  10. line 15: must say name = to store the result of substring
  11. line 16: must use parentheses () when calling length

Exercise - Corrected version

public class StringOops {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Type your name: ");
        String name = console.next();
        process(name);
    }

    public static void process(String "name") {
        if (name.equals("Whitaker")) {
            System.out.println("You must be really awesome.");
        }
        name = name.replace("a", "e");
        name = name.toUpperCase();
        name = name.substring(0, 3);
        System.out.println(name + " has " + name.length() + " letters");
    }
}

Exercise : CollegeAdmit practice-it

Write a complete program CollegeAdmit with the behavior shown below. Use the Scanner to read user input for a student's grade point average and SAT exam score. A GPA below 1.8 will cause the student to be rejected; an SAT score below 900 will also cause a rejection. Otherwise the student is accepted.

University admission program
What is your GPA? 3.2
What is your SAT score? 1280
You were accepted!

Check your answer using Practice-It from the check-mark icon above.

Exercise : Scanner sum

Copy and paste the following code into jGrasp.

public class SumNumbers {
    public static void main(String[] args) {
        int low = 1;
        int high = 1000;
        int sum = 0;
        for (int i = low; i <= high; i++) {
            sum += i;
        }
        System.out.println("sum = " + sum);
    }
}

continued on next slide...

Exercise : Scanner sum

Modify the code to use a Scanner to prompt the user for the values of low and high. Below is a sample execution in which the user asks for the same values as in the original program (1 through 1000):

low? 1
high? 1000
sum = 500500

Below is an execution with different values for low and high:

low? 300
high? 5297
sum = 13986903

You should exactly reproduce this format.

Exercise : longestName practice-it

Write a method named longestName that reads names typed by the user and prints the longest name (the name that contains the most characters) in the format shown below. Your method should accept a console Scanner and an integer n as parameters and should then prompt for n names.

A sample execution of the call longestName(console, 4) might look like the following:

name #1? roy
name #2? DANE
name #3? sTeFaNiE
name #4? Erik
Stefanie's name is longest

Try to solve this problem in Practice-It: click on the check-mark above!