CSU Banner

CS 150, Spring 2018

Lab 7 - Introduction to Strings

Wednesday - February 28, 2018


Lab 6 Regrade: If you need a regrade for last week's lab, please open Lab 6 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the quiz last week for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. Practice calling Scanner methods with String input, and
  2. play around with several methods of the String class.

Getting Started

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

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 such as:

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. Lastly, close your Scanner.
  17. 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



© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building