CS 150, Fall 2017
Lab 5 - Introduction to Strings
Monday - September 25, 2017
Objectives of this Lab
- Review how to use a Scanner object to read input,
- practice calling Scanner methods with String input, and
- 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:
- Declaring and initializing Scanner objects
- 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
- toUpperCase() - converts letters in the string to uppercase
- toLowerCase() - converts letter in the string to lowercase
- + - operator used for string concatenation
Today's assignment
- 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).
- 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
- Make sure your R5.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.