CS 163/164, Summer 2018
Lab 6 - Scanners, Strings, Characters
Wednesday, Jun. 20th
Objectives of this Lab
- Review how to use a Scanner object to read input,
- practice calling methods on a String object, and
- play around with the character primitive type.
Getting Started
Create a new Java Project named R6, and make a class named R6.
Overview of Scanner Methods
Your TA will review the Scanner class and the concept of input and output streams:
- 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
- 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
Overview of Character Primitive
Your TA will discuss character primitives and the underlying ASCII representation:
- Declaring and initializing char primitives
- Converting char to another integer type (ASCII)
- Character wrapper class and the following methods:
- isDigit() - returns true if char is '0'..'9'
- isWhitespace() - returns true if char is white space
- isLowerCase() - returns true if char is 'a'..'z'
- isUpperCase() - returns true if char is 'A'..'Z'
- isLetter() - returns true if char is 'A'..'Z' or 'a'..'z'
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.
- Make sure your R6.java compiles and produces the correct output.
- Your output should have 10 lines of output, including the prompts.
- Have your TA check your output for credit.