CSU Banner

CS 150, Fall 2017

Lab 5 - Introduction to Strings

Monday - September 25, 2017


Objectives of this Lab

  1. Review how to use a Scanner object to read input,
  2. practice calling Scanner methods with String input, and
  3. play around with several methods of the String class.

Getting Started

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

Overview of Scanner Methods

Your TA will review several methods of the Scanner class and show you where you can find documentation for any method of the Scanner class:

Overview of String Methods

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

Today's assignment

  1. Declare three String variables called myString0, myString1, and myString2 and initialize all three to empty Strings ("").
  2. Note: The syntax of the empty String does not include a space between the double quotes.
  3. Declare and initialize a Scanner object to read from the keyboard.
  4. Prompt the user with the following String literal: "myString0 = ".
  5. Reassign myString0 using the next() Scanner method of your Scanner object. When you run the program enter "Java" for myString0.
  6. Prompt the user a second time with the following String literal: "myString1 = ".
  7. Reassign myString1 using the next() Scanner method of your Scanner object. When you run the program enter "Programming" for myString1.
  8. Prompt the user a final time with the following String literal: "myString2 = ".
  9. Reassign myString2 using the next() Scanner method of your Scanner object. When you run the program enter "Language" for myString2.
  10. Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".".
  11. Print the sum of the lengths of myString1 and myString2 (must call the length() method).
  12. Print the 2nd and 7th character of myString1 (must use the charAt() method), separated by a comma.
  13. Print the index of 'a' in myString0 (must use the indexOf() method).
  14. Print myString2 converted to uppercase (must use the toUpperCase() method).
  15. Print the 3rd through 8th character of myString1 (must use the substring() method).
  16. Note: Chapter 4 lecture slides, starting on page 6, have many of the methods explained that are implemented above.

Console Output

myString0 = Java 
myString1 = Programming 
myString2 = Language 
Java is a Programming Language.
19
r,m
1
LANGUAGE
ogramm



CS Banner
CS Building