Working with Strings
The following program illustrates the use of some of the methods in
the String class. Study the program to see what it does.
// ***************************************************************
// StringManips.java
//
// Test several methods for manipulating String objects
// ***************************************************************
import java.util.Scanner;
public class StringManips
{
public static void main (String[] args)
{
String phrase = new String ("This is a String test.");
int phraseLength; // number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
phraseLength = phrase.length(); //Line A
middleIndex = phraseLength / 2;
firstHalf = phrase.substring(0,middleIndex);
secondHalf = phrase.substring(middleIndex, phraseLength);
switchedPhrase = secondHalf+firstHalf; //Line B
// print information about the phrase
System.out.println();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
System.out.println ("Index of the middle: " + middleIndex);
System.out.println ("Character at the middle index: " +
phrase.charAt(middleIndex));
System.out.println ("Switched phrase: " + switchedPhrase);
System.out.println();
}
}
The file
StringManips.java contains this program.
Save the file to your directory and compile and run it.
Study the output and make sure you understand the relationship between
the code and what is printed. Place a copy of code from 'Line A' to 'Line B' in your lab document and annotate the lines to indicate (briefly) what each is doing. Now modify the Java file as follows:
- Declare a variable of type String named middle3 (put your
declaration with the other declarations near the top of the program)
and use an assignment statement and the substring method
to assign to middle3 the substring consisting of the middle
three characters of phrase (the character at the middle index
together with the character to the left of that and the one to the
right). Add a println statement
to print out the result. Save, compile, and run to test what you have
done so far.
- Add an assignment statement to replace all blank characters in
switchedPhrase with an asterisk (*). The result should be
stored back in switchedPhrase (so switchedPhrase is actually
changed). (Do not add another print -- place your statement in the
program so that this new value of switchedPhrase will be
the one printed in the current println statement.) You'll want to use the replace method of the String class. (This is a good time to learn to read Java's on-line documentation. Just search it for "replace" and read the associated explanation.) Save, compile, and
run your program.
- Declare two new String variables city and state. Add
statements to the program to prompt the user to enter the values for these Strings: the names of their home city and home state. Read in the results using Scanner's nextLine method. Then use String methods to create and print a new string that
consists of the state name (all in uppercase letters) followed by the city
name (all in lowercase letters) followed again by the state name (uppercase).
To get a string from the user, use a Scanner, like we did in the Circle program, and its nextLine method. Assuming the name of the Scanner is myInput, you'd first prompt the user to enter a value (such as the city name), then use code such as
String city = myInput.nextLine();
So, if the user enters Lilesville for the city and North Carolina
for the state, the program should create and print the string
NORTH CAROLINAlilesvilleNORTH CAROLINA
(Notice that the case of the output string might differ from that entered by the user.)
Submit a copy of your code, and copy the output of two separate runs of your program into the lab document.