CSU Banner

CS 163/164, 🀨 Fall 2019 🀨

Lab 8 - Scanners, Strings, Characters

Tuesday, Sep. 24th or Wednesday, Sep. 25th


Objectives of this Lab

  1. Review how to use a Scanner object to read input,
  2. practice calling methods on a String object, and
  3. play around with the character primitive type.

Getting Started

Create a new Java Project named Strings, and make a class named Strings.

Overview of Scanner Methods

Your TA will review the Scanner class and the concept of input and output streams:

Overview of String Methods

Your TA will discuss the String class and the following methods:

Overview of Character Primitive

Your TA will discuss character primitives and the underlying ASCII representation:

Today's assignment

A) Declare a String variable called myString0 and initialize it to "Java".
B) Declare a String variable called myString1 and initialize it to "Programming".
C) Declare a String variable called myString2 and initialize it to "Language".
D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".".
E) Print the sum of the lengths of myString1 and myString2 (must call the length() method).
F) Print the 2nd, 4th, and 7th character of myString1 (must use the charAt() method), separated by commas.
G) Print the index of 'a' in myString0 (must use the indexOf() method).
H) Print myString2 converted to uppercase (must use the toUpperCase() method).
I) Print the 3rd through 8th character of myString1 (must use the substring() method).
J) Declare a String called myString3 and set it to "Whatever!", you must use the 'new' operator.
K) Declare a String called myString4 and set it to "Whatever!", you must use the 'new' operator.
L) Print a comparison of the two strings using (myString3 == myString4).
M) Print a comparison of the two strings using (myString3.equals(myString4)).
N) Declare three char variables c0, c1, and c2 with the values '^', 'G', and '7'.
O) Print the characters, separated by semicolons.
P) Print the ASCII value of the characters, separated by commas.
HINT: Use a typecast of the character from char to int.



CS Banner
CS Building