Building Java Programs

Lab: Strings

Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.

Lab goals

Goals for this problem set:

String methods

Method name Description
charAt(index) character at given index
indexOf(str) index where the start of the given String appears in this string (-1 if not found)
length() number of characters in this String
replace(str1, str2) a new string with all occurrences of str1 changed to str2
substring(index1, index2)
or substring(index1)
the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string
toLowerCase() a new string with all lowercase letters
toUpperCase() a new string with all uppercase letters

Exercise : String expressions practice-it

Write the results of each expression with Strings in "quotes" and characters in single quotes ('a')

//       index 0123456789012345
String str1 = "Frodo Baggins";
String str2 = "Gandalf the GRAY";
str1.length()
13
str1.charAt(7)
'a'
str2.charAt(0)
'G'
str1.indexOf("o")
2
str2.toUpperCase()
"GANDALF THE GRAY"
str1.toLowerCase().indexOf("B")
-1
str1.substring(4)
"o Baggins"
str2.substring(3, 14)
"dalf the GR"
str2.replace("a", "oo")
"Goondoolf the GRAY"
str2.replace("gray", "white")
"Gandalf the GRAY"
"str1".replace("r", "range")
"strange1"

Exercise : Syntax errors

Exercise - answer

  1. line 5: nextString should be next
  2. line 9: string should be String
  3. line 9: name should not be in quotes
  4. line 10: Whitaker should be in quotes
  5. line 10: cannot compare strings with ==; must use .equals
  6. line 13: cannot call replace without specifying a string object (name)
  7. line 14: toUppercase should be toUpperCase
  8. line 14: name. should come before toUpperCase, not passed as a parameter to it
  9. line 14: must say name = to store the result of toUpperCase
  10. line 15: must say name = to store the result of substring
  11. line 16: must use parentheses () when calling length

Exercise - Corrected version

public class StringOops {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Type your name: ");
        String name = console.next();
        process(name);
    }

    public static void process(String "name") {
        if (name.equals("Whitaker")) {
            System.out.println("You must be really awesome.");
        }
        name = name.replace("a", "e");
        name = name.toUpperCase();
        name = name.substring(0, 3);
        System.out.println(name + " has " + name.length() + " letters");
    }
}

Exercise : ProcessName practice-it

Copy/paste and save icon ProcessName.java in your editor, then go to the next slide.

import java.util.*;  // for Scanner

public class ProcessName {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Type your name: ");
        
        // your code goes here
        
        System.out.println("Your name is: " + name);
    }
}

continued on the next slide ...

Exercise - code to add practice-it