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
- Practice calling Scanner methods with String input, and
- play around with several methods of the String class.
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:
- nextInt() - reads an integer
- nextDouble() - reads a double
- next() - reads a token
- nextLine() - reads a line of text
- hasNext() - checks if there is another token (make a note of this one for a future assignment)
- hasNextDouble() - checks if there is a double next
- close() - closes scanner
Overview of String Methods
Your TA will discuss the String class and the following methods:
- Declaring and initializing String objects (with and without new keyword)
- length() - returns the length of the string
- indexOf() - returns the index of the specified character
- charAt() - returns the character at the specified index
- substring() - returns a portion of the string, 2 usage options
- toUpperCase() - converts letters in the string to uppercase
- toLowerCase() - converts letter in the string to lowercase
- + - operator used for string concatenation
- Declare three String variables called myString0, myString1, and myString2 and initialize all three to empty Strings ("").
- Note: The syntax of the empty String does not include a space between the double quotes.
- Declare and initialize a Scanner object to read from the keyboard.
- Prompt the user with the following String literal: "myString0 = ".
- Reassign myString0 using the next() Scanner method of your Scanner object. When you run the program enter "Java" for myString0.
- Prompt the user a second time with the following String literal: "myString1 = ".
- Reassign myString1 using the next() Scanner method of your Scanner object. When you run the program enter "Programming" for myString1.
- Prompt the user a final time with the following String literal: "myString2 = ".
- Reassign myString2 using the next() Scanner method of your Scanner object. When you run the program enter "Language" for myString2.
- Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".".
- Print the sum of the lengths of myString1 and myString2 (must call the length() method).
- Print the 2nd and 7th character of myString1 (must use the charAt() method), separated by a comma.
- Print the index of 'a' in myString0 (must use the indexOf() method).
- Print myString2 converted to uppercase (must use the toUpperCase() method).
- Print the 3rd through 8th character of myString1 (must use the substring() method).
- Lastly, close your Scanner.
- Note: Chapter 4 lecture slides, starting on page 6, have many of the methods explained that are implemented above.
myString0 = Java
myString1 = Programming
myString2 = Language
Java is a Programming Language.
- Make sure your Lab7.java compiles and produces the correct output.
- Your output should have 9 lines of output, including the prompts.
- Have your TA check your output for credit and take the Canvas attendance quiz.
© 2018 CS150 Colorado State University. All Rights Reserved.