A Problem Solved: Extracting Strings
In this lesson, we will explore how we debugged a program to solve a given problem.
We'll cover the following...
Problem statement
Read a person’s name as a string, compute its length, identify the first and last names, and obtain the person’s initials. If the name is typed on one line, insist that only one space occur between the two names.
We already solved this problem in the chapter Using Classes and Objects, but this time we’ll make some mistakes and see how to find them.
First-try solution
The program given below shows our first attempt at a solution. Click the RUN button. Can you explain what happens?
import java.util.Scanner; public class ProblemSolved { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please type your first name, a space, and " + "your last name:"); String name = keyboard.nextLine(); // Get indices: int spaceIndex = name.indexOf(" "); // Space between first and last names int firstNameStart = 1; int firstNameEnd = spaceIndex - 1; int lastNameStart = spaceIndex + 1; int lastNameEnd = firstNameStart + name.length() - 1; String firstName = name.substring(firstNameStart, firstNameEnd); String lastName = name.substring(lastNameStart, lastNameEnd); int nameLength = firstName.length() + lastName.length(); char firstInitial = firstName.charAt(1); char lastInitial = lastName.charAt(1); String initials = firstInitial + ". " + lastInitial + "."; System.out.println("Hi, " + name + ".\nYour name contains " + nameLength + " characters."); System.out.println("Your first name is " + firstName + "."); System.out.println("Your last name is " + lastName + "."); System.out.println("Your initials are " + initials); } // End main } // End ProblemSolved
Although the program compiles and executes, it produces output like this:
Please type your first name, a space, and your last name:Jamie Java Hi, Jamie Java. Your name contains 7 characters. Your first name is ami. Your last name is Java. Your initials are m. a.
Our logic identifies the full name correctly, but its length is wrong. Also incorrect are the extracted first name and the initials. As we study the code, we realize that we mistakenly begin the string at index 1. Actually, the first character in a string is at index 0.
Take two
We need to change the arguments to the method charAt
from 1 to 0 in lines 22 and 23 of the code given above. Although we might be tempted to run the program again, first we really should see whether we made this same mistake elsewhere. In fact, the statement in line 13 assigns 1 instead of 0 to firstNameStart
. So let’s make the following three changes to the program:
13 int firstNameStart = 0;
. . .
22 char firstInitial = firstName.charAt(0);
23 char lastInitial = lastName.charAt(0);
Click the RUN button!
import java.util.Scanner; public class ProblemSolved { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please type your first name, a space, and " + "your last name:"); String name = keyboard.nextLine(); // Get indices: int spaceIndex = name.indexOf(" "); // Space between first and last names int firstNameStart = 0; int firstNameEnd = spaceIndex - 1; int lastNameStart = spaceIndex + 1; int lastNameEnd = firstNameStart + name.length() - 1; String firstName = name.substring(firstNameStart, firstNameEnd); String lastName = name.substring(lastNameStart, lastNameEnd); int nameLength = firstName.length() + lastName.length(); char firstInitial = firstName.charAt(0); char lastInitial = lastName.charAt(0); String initials = firstInitial + ". " + lastInitial + "."; System.out.println("Hi, " + name + ".\nYour name contains " + nameLength + " characters."); System.out.println("Your first name is " + firstName + "."); System.out.println("Your last name is " + lastName + "."); System.out.println("Your initials are " + initials); } // End main } // End ProblemSolved
When we run the program this time, we get the following output:
Please type your first name, a space, and your last name:Jamie Java Hi, Jamie Java. Your name contains 7 characters. Your first name is Jami. Your last name is Jav. Your initials are J. J.
This time, the output begins as it did before: The full name is correct, and its length is ...