Strings and Text I/O

advertisement
Computer Science Notes
Chapter 8
Page 1 of 19
Chapter 8: Strings and Text I/O
These notes are meant to accompany Introduction to Java Programming: Brief Version, seventh edition by Y.
Daniel Lang.
Programming Skills in a Nutshell:
At the end of this chapter you should have the following programming skills:
1. To use more methods from the String class.
2. To open a file for either reading or writing using the Scanner and PrintWriter classes,
respectively.
3. To read or write data to file using the println() and next() methods of the Scanner and
PrintWriter classes, respectively.
4. To close a file once you are done with it.
package encryption;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* The EncryptionTool class contains methods for encrypting and decrypting
* text files using a simple rotation (Caesar cipher) technique.
* @author Kevin Mirus
*/
public class EncryptionTool
{
/**The first character to be used in the rotation cipher*/
public static final char FIRST_CHAR = ' ';
/**The last character to be used in the rotation cipher*/
public static final char LAST_CHAR = '~';
public static final int CHAR_SPAN = LAST_CHAR - FIRST_CHAR;
/**
* Encrypts a given File by rotating characters by a given key value.
* The encrypted file will have the source file's name with an extension of
.rot
* @param key the key value
* @param sourceFile the File that will be encrypted
*/
public static void rotationEncrpyt(int key, File sourceFile) throws
Exception
{
//Create a Scanner object to read in from the sourceFile.
Scanner diskInput = new Scanner(sourceFile);
//Create a target File for output.
File targetFile = new File (sourceFile.getName() + ".rot");
//Create a PrintWriter object to do the writing to targetFile.
PrintWriter diskOutput = new PrintWriter(targetFile);
//Process every line in the input file.
while (diskInput.hasNextLine())
{
Computer Science Notes
Chapter 8
Page 2 of 19
//Read in the next line of text.
String inputLine = diskInput.nextLine();
//Create a String to store the encrypted line of text.
String outputLine = "";
//Loop through every character of the inputLine and encrypt it
using rotation.
for (int i = 0; i < inputLine.length(); i++)
{
int nextChar = (int) inputLine.charAt(i);
nextChar = nextChar + key;
if (nextChar > LAST_CHAR)
nextChar = nextChar - LAST_CHAR + FIRST_CHAR - 1;
outputLine = outputLine + (char) nextChar;
}
//Write the encrypted line of text to the output file.
diskOutput.println(outputLine);
}
//Close both input and output files.
diskInput.close();
diskOutput.close();
}//end of rotationEncrpyt(key, File)
/**
* Decrypts a given File by "un-"rotating characters by a given key value.
* It is expected that the encrypted file has an extension of .rot;
* the decrypted file's name will be the encrypted file's name less the
extension .rot
* @param key the key value that was used to encrypt the file
* @param sourceFile the File that will be decrypted
*/
public static void rotationDecrypt(int key, File sourceFile) throws
Exception
{
//Create a Scanner object to read in from the sourceFile.
Scanner diskInput = new Scanner(sourceFile);
//Create a target File for output.
File targetFile = new File (sourceFile.getName().substring(0,
sourceFile.getName().length()-4));
//Create a PrintWriter object to do the writing to targetFile.
PrintWriter diskOutput = new PrintWriter(targetFile);
//Process every line in the input file.
while (diskInput.hasNextLine())
{
//Read in the next line of text.
String inputLine = diskInput .nextLine();
//Create a String to store the decrypted line of text.
String outputLine = "";
Computer Science Notes
Chapter 8
Page 3 of 19
//Loop through every character of the inputLine and decrypt it
using rotation.
for (int i = 0; i < inputLine.length(); i++)
{
int nextChar = (int) inputLine.charAt(i);
nextChar = nextChar - key;
if (nextChar < FIRST_CHAR)
nextChar = LAST_CHAR - (FIRST_CHAR - nextChar);
outputLine = outputLine + (char) nextChar;
}
//Write the decrypted line of text to the output file.
diskOutput.println(outputLine);
}
//Close both input and output files.
diskInput .close();
diskOutput .close();
}//end of rotationDecrpyt(int, File)
/**
* "Deciphers" a given File that was encrypted using an unknown rotation key
* by reading up to the first five lines of the encrypted file, then
applying
* all possible rotation keys to that file. The results are returned as a
* two-dimensional String array for the user to peruse and decide upon the
proper
* rotation key.
* It is expected that the encrypted file has an extension of .rot.
* @param sourceFile the File that will be deciphered
*/
public static String[][] rotationDecipher(File sourceFile) throws Exception
{
String[][] possibleSolution = new String[5][CHAR_SPAN + 1];
//Create a Scanner object to read in from the sourceFile.
Scanner diskInput = new Scanner(sourceFile);
//Process up to the first five lines in the input file.
int numLinesRead = 0;
while (diskInput .hasNextLine() && numLinesRead<5)
{
//Read in the next line of text.
String inputLine = diskInput.nextLine();
//Loop through every character of the inputLine
for (int i = 0; i < inputLine.length(); i++)
{
//Loop through every possible key.
for (int key = 0; key < CHAR_SPAN + 1; key++)
{
if (key == 0) possibleSolution[numLinesRead][key]="";
int nextChar = (int) inputLine.charAt(i);
Computer Science Notes
Chapter 8
Page 4 of 19
nextChar = nextChar - key;
if (nextChar < FIRST_CHAR)
nextChar = LAST_CHAR - (FIRST_CHAR - nextChar) +
1;
possibleSolution[numLinesRead][key] += (char)
nextChar;
}
}
numLinesRead++;
}
//Close the input file.
diskInput.close();
return possibleSolution;
}//end of rotationDecipher(File)
}//end of class EncryptionTool
package encryption;
import java.io.File;
import java.util.Scanner;
/**
* /** The EncryptionToolUserInterface class implements an application that uses
the
* EncryptionTool class to encrypt and decrypt text files.
* @author Kevin Mirus
*
*/
public class EncryptionToolUserInterface
{
/** Prompts the user for encryption or decryption, the key, and the file.
* @param argv is not used.
*/
public static void main(String[] argv) throws Exception
{
//Tell the user what the program does
System.out.println("This program will encrypt or decrypt text files
using a rotation cipher.");
Scanner input = new Scanner(System.in);
int continueConversions = 1;
String filename = "";
int key = 13;
do
{
System.out.println("Enter 1 if you want to encrypt an existing
file.");
System.out.println("Enter 2 if you want to decrypt an existing
Computer Science Notes
Chapter 8
Page 5 of 19
file.");
System.out.println("Enter 3 if you want to decipher an existing file
for which you don't know the key.");
int menuChoice = input.nextInt();
System.out.println("**************************************************");
switch (menuChoice)
{
case 1: //encrypt an existing file
System.out.println("Enter the file name.");
filename = input.next();
System.out.println("Enter the rotation key.");
key = input.nextInt();
File encryptFile = new File (filename);
if (encryptFile.exists())
{
EncryptionTool.rotationEncrpyt(key, encryptFile);
System.out.println(filename + " successfully
encrypted.");
}
else
System.out.println(filename + " does not exist.");
break;
case 2: //decrypt an existing file
System.out.println("Enter the file name.");
filename = input.next();
System.out.println("Enter the rotation key.");
key = input.nextInt();
File decryptFile = new File (filename);
if (decryptFile.exists())
{
EncryptionTool.rotationDecrypt(key, decryptFile);
System.out.println(filename + " successfully
decrypted.");
}
else
System.out.println(filename + " does not exist.");
break;
case 3: //decipher an existing file of unknown key
System.out.println("Enter the file name.");
filename = input.next();
File decipherFile = new File (filename);
if (decipherFile.exists())
{
String[][] possibleSolution =
EncryptionTool.rotationDecipher(decipherFile);
for (key = 0; key < EncryptionTool.CHAR_SPAN+1; key++)
{
System.out.println("\nHere is the decryption for
a key = " + key);
Computer Science Notes
Chapter 8
Page 6 of 19
for (int lineNumber = 0; lineNumber<5;
lineNumber++)
{
if (possibleSolution[lineNumber][key] !=
null)
System.out.println(possibleSolution[lineNumber][key]);
}
}
System.out.println();
}
else
System.out.println(filename + " does not exist.");
break;
default:
System.out.println("That was not a valid menu choice.");
}
System.out.println("\n**************************************************");
System.out.println("Enter 1 to run another file conversion or 0 to
quit.");
continueConversions = input.nextInt();
} while(continueConversions == 1);
System.out.println("Encryption program terminated.");
}//end method main()
}//end class EncryptionToolUserInterface
Book’s Statement of Skills:
1. To use the String class to process fixed strings. (8.2)
2. To use the Character class to process a single character. (8.3)
3. To use the StringBuilder and StringBuffer classes to process flexible strings. (8.4)
4. To distinguish between the String, StringBuilder, and StringBuffer classes. (8.2 – 8.4)
5. To learn how to pass strings to the main method from the command line. (8.5)
6. To discover file properties and to delete and rename files using the File class. (8.6)
7. To write data to a file using the PrintWriter class. (8.7.1)
8. To read data from a file using the Scanner class. (8.7.2)
9. (Optional GUI) To open files using a dialog box. (8.8)
Section 8.1: Introduction
A string is a sequence of characters. In Java, a string is an object. The String, StringBuilder, and StringBuffer
classes are used for storing and processing strings. The String class is immutable; the StringBuilder and
StringBuffer classes are not. This chapter also illustrates how to process command-line arguments to the main
method (which is an application of processing a String array), and how to perform simple text output and input
using the Scanner and PrintWriter classes (respectively)
Computer Science Notes
Chapter 8
Page 7 of 19
Section 8.2: The String Class
 The java.lang.String class models a sequence of characters as a string.
 The String class has 13 constructors and 53 methods…
 See http://java.sun.com/javase/6/docs/api/java/lang/String.html
 Some common String operations are:

Isolating individual characters

Comparing Strings

Searching for and manipulating substrings

Converting characters to upper case or lower case

java.lang.String
+CASE_INSENSITIVE_ORDER: Comparator
+String()
+String(bytes: byte[])
+String(bytes: byte[], charset: Charset )
+String(bytes: byte[], offset: int, length: int)
+String(bytes: byte[], offset: int, length: int,
charset: Charset)
+String(bytes: byte[], offset: int, length: int,
charsetName: String)
+String(bytes: byte[], charsetName: String)
+String(value: char[])
+String(value: char[], offset: int, count: int)
+String(codePoints: int[], offset: int, count:
int)
+String(original: String)
+String(buffer: StringBuffer)
+String(builder: StringBuilder)
+charAt(index: int): char
+charAt(int index): char
+codePointAt(int index): int
codePointBefore(int index): int
The String class represents character strings. All string literals
in Java programs, such as "abc", are implemented as
instances of this class. Strings are constant; their values
cannot be changed after they are created. String buffers
support mutable strings.
A Comparator that orders String objects as by
compareToIgnoreCase.
Constructs a default String.
Constructs a new String by decoding the specified array of
bytes using the platform's default charset.
Constructs a new String by decoding the specified array of
bytes using the specified charset.
Constructs a new String by decoding the specified subarray of
bytes using the platform's default charset.
Constructs a new String by decoding the specified subarray of
bytes using the specified charset.
Constructs a new String by decoding the specified subarray of
bytes using the specified charset.
Constructs a new String by decoding the specified array of
bytes using the specified charset.
Allocates a new String so that it represents the sequence of
characters currently contained in the character array argument.
Allocates a new String that contains characters from a
subarray of the character array argument.
Allocates a new String that contains characters from a
subarray of the Unicode code point array argument.
Initializes a newly created String object so that it represents the
same sequence of characters as the argument; in other words,
the newly created string is a copy of the argument string.
Allocates a new string that contains the sequence of
characters currently contained in the string buffer argument.
Allocates a new string that contains the sequence of
characters currently contained in the string builder argument.
Returns the char value at the specified index.
Returns the char value at the specified index.
Returns the character (Unicode code point) at the specified
index.
Returns the character (Unicode code point) before the
specified index.
Computer Science Notes
Chapter 8
+codePointCount(int beginIndex, int
endIndex): int
+compareTo(String anotherString): int
+compareToIgnoreCase(String str): int
+concat(String str): String
+contains(CharSequence s): boolean
+contentEquals(CharSequence cs): boolean
+contentEquals(StringBuffer sb): boolean
+copyValueOf(char[] data): String
+copyValueOf(char[] data, int offset, int
count): String
+endsWith(String suffix): boolean
+equals(Object anObject): boolean
+equalsIgnoreCase(String anotherString):
boolean
format(Locale l, String format, Object... args):
String
+format(String format, Object... args): String
+getBytes():byte[]
+getBytes(Charset charset): byte[]
+getBytes(String charsetName): byte[]
+getChars(int srcBegin, int srcEnd, char[]
dst, int dstBegin): void
+hashCode(): int
+indexOf(int ch): int
+indexOf(int ch, int fromIndex): int
+indexOf(String str): int
+indexOf(String str, int fromIndex): int
+intern():String
+isEmpty(): boolean
+lastIndexOf(int ch): int
+lastIndexOf(int ch, int fromIndex): int
+lastIndexOf(String str): int
Page 8 of 19
Returns the number of Unicode code points in the specified
text range of this String.
Compares two strings lexicographically.
Compares two strings lexicographically, ignoring case
differences.
Concatenates the specified string to the end of this string.
Returns true if and only if this string contains the specified
sequence of char values.
Compares this string to the specified CharSequence.
Compares this string to the specified StringBuffer
Returns a String that represents the character sequence in the
array specified.
Returns a String that represents the character sequence in the
array specified.
Tests if this string ends with the specified suffix.
Compares this string to the specified object.
Compares this String to another String, ignoring case
considerations.
Returns a formatted string using the specified locale, format
string, and arguments.
Returns a formatted string using the specified format string and
arguments.
Encodes this String into a sequence of bytes using the
platform's default charset, storing the result into a new byte
array.
Encodes this String into a sequence of bytes using the given
charset, storing the result into a new byte array.
Encodes this String into a sequence of bytes using the named
charset, storing the result into a new byte array.
Copies characters from this string into the destination
character array.
Returns a hash code for this string.
Returns the index within this string of the first occurrence of the
specified character.
Returns the index within this string of the first occurrence of the
specified character, starting the search at the specified index.
Returns the index within this string of the first occurrence of the
specified substring.
Returns the index within this string of the first occurrence of the
specified substring, starting at the specified index.
Returns a canonical representation for the string object.
Returns true if, and only if, length() is 0.
Returns the index within this string of the last occurrence of the
specified character.
Returns the index within this string of the last occurrence of the
specified character, searching backward starting at the
specified index.
Returns the index within this string of the rightmost occurrence
of the specified substring.
Computer Science Notes
Chapter 8
+lastIndexOf(String str, int fromIndex): int
+length():int
+matches(String regex): boolean
+offsetByCodePoints(int index, int
codePointOffset): int
+regionMatches(boolean ignoreCase, int
toffset, String other, int ooffset, int len):
boolean
+regionMatches(int offset, String other, int
offset, int len): boolean
+replace(char oldChar, char newChar):
String
+replace(CharSequence target,
CharSequence replacement): String
+replaceAll(String regex, String
replacement): String
+replaceFirst(String regex, String
replacement): String
+split(String regex): String[]
+split(String regex, int limit): String[]
+startsWith(String prefix): boolean
+startsWith(String prefix, int toffset): boolean
+subSequence(int beginIndex, int endIndex):
CharSequence
+substring(int beginIndex): String
+substring(int beginIndex, int endIndex):
String
+toCharArray():char[]
+toLowerCase():String
+toLowerCase(Locale locale): String
+toString():String
+toUpperCase():String
+toUpperCase(Locale locale): String
+trim():String
+valueOf(boolean b): String
+valueOf(char c): String
+valueOf(char[] data): String
+valueOf(char[] data, int offset, int count):
String
Page 9 of 19
Returns the index within this string of the last occurrence of the
specified substring, searching backward starting at the
specified index.
Returns the length of this string.
Tells whether or not this string matches the given regular
expression.
Returns the index within this String that is offset from the given
index by codePointOffset code points.
Tests if two string regions are equal.
Tests if two string regions are equal.
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
Replaces each substring of this string that matches the literal
target sequence with the specified literal replacement
sequence.
Replaces each substring of this string that matches the given
regular expression with the given replacement.
Replaces the first substring of this string that matches the
given regular expression with the given replacement.
Splits this string around matches of the given regular
expression.
Splits this string around matches of the given regular
expression.
Tests if this string starts with the specified prefix.
Tests if the substring of this string beginning at the specified
index starts with the specified prefix.
Returns a new character sequence that is a subsequence of
this sequence.
Returns a new string that is a substring of this string.
Returns a new string that is a substring of this string.
Converts this string to a new character array.
Converts all of the characters in this String to lower case using
the rules of the default locale.
Converts all of the characters in this String to lower case using
the rules of the given Locale.
This object (which is already a string!) is itself returned.
Converts all of the characters in this String to upper case using
the rules of the default locale.
Converts all of the characters in this String to upper case using
the rules of the given Locale.
Returns a copy of the string, with leading and trailing
whitespace omitted.
Returns the string representation of the boolean argument.
Returns the string representation of the char argument.
Returns the string representation of the char array argument.
Returns the string representation of a specific subarray of the
char array argument.
Computer Science Notes
+valueOf(double d): String
+valueOf(float f): String
+valueOf(int i): String
+valueOf(long l): String
+valueOf(Object obj): String
Chapter 8
Page 10 of 19
Returns the string representation of the double argument.
Returns the string representation of the float argument.
Returns the string representation of the int argument.
Returns the string representation of the long argument.
Returns the string representation of the Object argument.
Section 8.2.1: Constructing a String

You can construct a new String object from a string literal:
o Syntax: String newString = new String(stringLiteral);
o Example: String newString = new String(My name is Kevin.);

Since string literals are treated as String objects, you can also create a String as:
o Example: String newString = My name is Kevin.;

You can construct a new String object from an array of characters:
o Example:
char[] charArray = {H, e, l, l, o, ,,  , D, o, o, f, u,
s, };
String newString = new String(charArray);
Section 8.2.2: Immutable Strings and Interned Strings

A String object is immutable, meaning its contents can not be changed. For example, the following
code does not change the value of any String object, but rather changes which String object the String
variable s refers to:
String s = Hello;
s = Goodbye.;

An interned string is a unique instance of a string literal with the same character sequence used by the
JVM to reduce memory load when the same string literal is used more than once in a program.

The intern() method of the String class returns an interned string.
Section 8.2.3: String Comparisons

Do not use the = = operator to determine if two strings have the same sequence of characters.

Instead, use the equals() method of the String class to determine if two strings have the same
sequence of characters.

The compareTo() method of the String class is used to “lexigraphically” compare (i.e., by
comparing ASCII values) whether one string comes before another one.

The equalsIgnoreCase() method of the String class determines if two strings have the same
sequence of characters, regardless of capitalization.

The compareToIgnoreCase() method of the String class determines whether one string comes
before another one, regardless of capitalization.

The regionMatches() method of the String class determines if two strings have the same
sequence of characters in a given region.

The startsWith() method of the String class determines if a strings starts with a given string.

The endsWith() method of the String class determines if a strings ends with a given string.
//import java.lang.String;
//Do not need above import because
//all classes from the java.lang package are imported autoatically.
Computer Science Notes
Chapter 8
Page 11 of 19
/**The StringComparisons class implements an application that
* performs various comparisons between strings.
* @author Kevin Mirus
*/
public class StringComparisons
{
/**Shows how various strings compare under methods from teh String class.
* @param argv is not used.
*/
public static void main(String[] argv)
{
//Declare first pair of strings to compare.
String s1 = "Hello.";
String s2 = "hello.";
//Declare second pair of strings to compare.
String s3 = "abc";
String s4 = "abg";
//Declare third pair of strings to compare.
String s5 = "Kevin";
String s6 = "Kevin";
//Declare fourth pair of strings to compare.
String s7 = "Goodbye.";
String s8 = "Goodbye!";
//Tell the user what the program does.
System.out.println("This program compares strings.\n");
System.out.println("String s1 = " + s1);
System.out.println("String s2 = " + s2);
System.out.println("Under the equals() method:");
if (s1.equals(s2))
System.out.println("s1 and s2 have THE SAME contents.");
else
System.out.println("s1 and s2 have DIFFERENT contents.");
System.out.println("Under the equalsIgnoreCase() method:");
if (s1.equalsIgnoreCase(s2))
System.out.println("s1 and s2 have THE SAME contents (ignoring
capitalization).");
else
System.out.println("s1 and s2 have DIFFERENT contents (ignoring
capitalization).");
System.out.println("\nString s3 = " + s3);
System.out.println("String s4 = " + s4);
System.out.println("Under the compareTo() method:");
System.out.println("s3.compareTo(s4) returns: " + s3.compareTo(s4));
System.out.println("s4.compareTo(s3) returns: " + s4.compareTo(s3));
System.out.println("s1.compareTo(s2) returns: " + s1.compareTo(s2));
System.out.println("s2.compareTo(s1) returns: " + s2.compareTo(s1));
System.out.println("String s5 = " + s5);
System.out.println("String s6 = " + s6);
System.out.println("s5.compareTo(s6) returns: " + s5.compareTo(s6));
System.out.println("String s7 = " + s7);
System.out.println("String s8 = " + s8);
System.out.println("s7.compareTo(s8) returns: " + s7.compareTo(s8));
System.out.println("\nUnder the compareToIgnoreCase() method:");
Computer Science Notes
Chapter 8
Page 12 of 19
System.out.println("s3.compareToIgnoreCase(s4)
s3.compareToIgnoreCase(s4));
System.out.println("s4.compareToIgnoreCase(s3)
s4.compareToIgnoreCase(s3));
System.out.println("s1.compareToIgnoreCase(s2)
s1.compareToIgnoreCase(s2));
System.out.println("s2.compareToIgnoreCase(s1)
s2.compareToIgnoreCase(s1));
System.out.println("s5.compareToIgnoreCase(s6)
s5.compareToIgnoreCase(s6));
returns: " +
returns: " +
returns: " +
returns: " +
returns: " +
System.out.println("\nUnder the regionMatches() method:");
System.out.println("s1.regionMatches(1, s2, 1, 3) returns: " +
s1.regionMatches(1, s2, 1, 3));
System.out.println("(because \"ell\" is the same thing as \"ell\")");
System.out.println("\nUnder the startsWith() method:");
System.out.println("s1.startsWith(\"Hell\") returns: " +
s1.startsWith("Hell"));
System.out.println("\nUnder the endsWith() method:");
System.out.println("s5.endsWith(\"vin\") returns: " + s5.endsWith("vin"));
}//end main(String)
}//end class StringComparisons
This program compares strings.
String s1
String s2
Under the
s1 and s2
Under the
s1 and s2
= Hello.
= hello.
equals() method:
have DIFFERENT contents.
equalsIgnoreCase() method:
have THE SAME contents (ignoring capitalization).
String s3 = abc
String s4 = abg
Under the compareTo() method:
s3.compareTo(s4) returns: -4
s4.compareTo(s3) returns: 4
s1.compareTo(s2) returns: -32
s2.compareTo(s1) returns: 32
String s5 = Kevin
String s6 = Kevin
s5.compareTo(s6) returns: 0
String s7 = Goodbye.
String s8 = Goodbye!
s7.compareTo(s8) returns: 13
Under the compareToIgnoreCase() method:
s3.compareToIgnoreCase(s4) returns: -4
s4.compareToIgnoreCase(s3) returns: 4
s1.compareToIgnoreCase(s2) returns: 0
s2.compareToIgnoreCase(s1) returns: 0
s5.compareToIgnoreCase(s6) returns: 0
Computer Science Notes
Chapter 8
Page 13 of 19
Under the regionMatches() method:
s1.regionMatches(1, s2, 1, 3) returns: true
(because "ell" is the same thing as "ell")
Under the startsWith() method:
s1.startsWith("Hell") returns: true
Under the endsWith() method:
s5.endsWith("vin") returns: true
Section 8.2.4: String Length, Characters, and Combining Strings

The length() method of the String class returns the number of characters in the string.

The charAt(index) method of the String class returns the character at position index, where
0  index  length() – 1.

The charAt() method is an indication of the fact that a string is represented internally as a private
array of characters.

To get a single character for input from the keyboard, you can use keyboard.next().charAt(0)


You can use the + operator to concatenate strings…
Example:
String s1 = Hello, ;
String s2 = Doofus. ;
String s3 = s1 + s2;


You also can use the concat() method of the String class to concatenate strings…
Example:
String s1 = Hello, ;
String s2 = Doofus. ;
String s3 = s1.concat(s2);
Section 8.2.5: Obtaining Substrings

The method substring(int beginIndex, int endIndex) of the String class returns a
new string that starts with the character at index beginIndex and ends at the character at index endIndex.

The method substring(int beginIndex) of the String class returns a new string that starts
with the character at index beginIndex and goes to the end of the string.
Section 8.2.7: Converting, Replacing, and Splitting Strings

The method toLowerCase() of the String class returns a new string with all alphabetic characters
converted to lower case.

The method toUpperCase() of the String class returns a new string with all alphabetic characters
converted to upper case.

The method trim() of the String class returns a new string with all leading and trailing whitespace
trimmed away.

The method replace(char oldChar, char newChar) of the String class returns a new
string with all occurrences of oldChar replaced with newChar.

The method replaceFirst(String regex, String newString) of the String class
returns a new string with the first occurrences of the regular expression regex replaced with newString.
Computer Science Notes
Chapter 8
Page 14 of 19

The method replaceAll(String regex, String newString) of the String class returns
a new string with every occurrence of the regular expression regex replaced with newString.

The method split(String regex, int limit) of the String class returns an array of
Strings where each string in the array is obtained by splitting the original string around matches of the given
regular expression (extract tokens from a string with the specified delimiter).
Example:
String[] tokens = Java#HTML#Perl.split(#, 0);
for (int i = 0; I < tokens.length; i++)
System.out.println(tokens[i] +  );
Produces as output:
Java HTML Perl
Section 8.2.7 Matching, Replacing, and Splitting by Patterns
A regular expression is a string that specifies a pattern in a text string to look for.

The method matches(String regex) of the String class returns whether or not the String
matches the regular expression regex.
Example: All the following return true because the substring .* of the regular expression Java.*
matches any zero or more characters.
Java is fun..matches(Java.*)
Java is cool..matches(Java.*)
Java is powerful..matches(Java.*)
Example: Check to see if two strings are valid social security numbers.
String validSSN = 111-11-1111;
System.out.println(validSSN.matches([\\d][ \\d][ \\d]- +
[ \\d][ \\d]-[ \\d][ \\d][ \\d][ \\d]));
String inValidSSN = 22-11-1111;
System.out.println(inValidSSN.matches([\\d][ \\d][ \\d]- +
[ \\d][ \\d]-[ \\d][ \\d][ \\d][ \\d]));

The method replaceAll(String regex, String replacement) of the String class
replaces all matches of the regular expression regex with the string replacement.
Example:
String s = a+b$#c.replaceAll([$+#],NNN);
System.out.println(s);
//returns aNNNbNNNNNNc
Java is cool..matches(Java.*)
Java is powerful..matches(Java.*)

The method split(String regex) of the String class returns an array of Strings where each
string in the array is obtained by splitting the original string at matches of the given regular expression.
Example:
String[] tokens = Java,C?C#,C++.split([.,:;?]);
for (int i = 0; i < tokens.length; i++)
System.out.println(tokens[i]);
//returns
Java
C
Computer Science Notes
Chapter 8
Page 15 of 19
C#
C++
***Same as Table 8.1 in intro6e p281
***with modifications
Table 1: Frequently Used Regular Expressions
Regular Expression
x
.
(ab|cd)
[abc]
[^abc]
Matches
Example
a specified character x
any single character
ab or cd
a, b, or c
any character except
a, b, or c
a through z
any character except
a through z
a through e or
m through p
intersection of a-e
with c-p
Java matches Java
Java matches J..a
ten matches t(en|im)
Java matches Ja[uvwx]a
Java matches Ja[^ars]a
\d
\D
\w
\W
\s
\S
a
a
a
a
a
a
Java2 matches "Java[\\d]"
$Java matches "[\\D][\\D]ava"
Java matches "[\\w]ava"
$Java matches "[\\W][\\w]ava"
"Java 2" matches "Java\\s2"
Java matches "[\\S]ava"
p*
zero or more
occurrences of pattern p
one or more
occurrences of pattern p
zero or one
occurrence of pattern p
exactly n
occurrences of pattern p
at least n
occurrences of pattern p
between n and m
occurrences (inclusive)
[a-z]
[^a-z]
[a-e[m-p]]
[a-e&&[c-p]]
p+
p?
p{n}
p{n,}
p{n,m}
digit, same as [0-9]
non-digit
word character
non-word character
whitespace character
non-whitespace char
Java matches [A-M]av[a-d]
Java matches Jav[^b-d]
Java matches
[A-G[I-M]]av[a-d]
Java matches
[A-P&&[I-M]]av[a-d]
Java matches "a*"
bbb matches "a*"
Java matches "a+"
bbb matches "a+"
Java matches "J?Java"
ava matches "J?ava"
Java matches "a{1}"
Java does not match "a{2}"
Java matches "a{1,}"
Java does not match "a{2,}"
Java matches "a{1,9}"
Java does not match "a{2,9}"
The above table is from
http://www.cs.armstrong.edu/liang/intro7e/supplement/SupplementRegularExpression.doc
Section 8.2.8 Finding a Character or a Substring in a String
Welcome to Java.indexOf(W)
returns
Welcome to Java.indexOf(o)
returns
Welcome to Java.indexOf(o, 5)
returns
Welcome to Java.indexOf(come)
returns
Welcome to Java.indexOf(Java)
returns
Welcome to Java.indexOf(Java, 5) returns
Welcome to Java.lastIndexOf(W)
returns
Welcome to Java.lastIndexOf(o)
returns
Welcome to Java.lastIndexOf(o, 5)returns
0.
4.
8.
3.
11.
-1.
0.
8.
4.
Computer Science Notes
Chapter 8
Page 16 of 19
Section 8.2.9 Conversion Between Strings and Arrays

The method toCharArray() of the String class converts a string into a character array
Example:
char[] chars = Java. toCharArray ();
for (int i = 0; i < chars.length; i++)
System.out.println(chars[i]);
//returns
J
a
v
a

The method getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
of the String class copies a substring of characters starting at index srcBegin and ending at index srcEnd
– 1 into array dst starting at index dstBegin.

The constructor String(char[]) of the String class creates a string containing all the characters
in the character array.

The static method valueOf(char[]) of the String class returns a string containing all the
characters in the character array.
Section 8.2.10 Converting Characters and Numeric Values to Strings

The static methods valueOf(char[]),valueOf (char) , valueOf(double) ,
valueOf(float) , valueOf(int), and valueOf(long) of the String class return a string
representation of the given character, array of characters, or number.

The static methods Double.parseDouble(String) and Integer.parseInt(String)
return a numerical representation of the given string.
Section 8.2.11 Problem: Checking Palindromes

See www.cs.armstrong.edu/liang/intro7e/book/CheckPalindrome.java
Section 8.3: The Character Class

Java provides a wrapper class for every primitive data type.

A wrapper class stores the primitive data type and provides several useful methods relating to that data
type.

The Character class is a wrapper class for the primitive char data type, and provides methods like
isLetter() that determine whether or not the character is an alphabetic letter.

See http://java.sun.com/javase/6/docs/api/java/lang/Character.html
Section 8.4 The StringBuilder/StringBuffer Classes

Instances are like a String object, but more flexible (you can mutate the data fields).

You can add, insert, or append new content into a StringBuffer/StringBuilder object.

The StringBuffer class is older, and should be used when it will be accessed by multiple tasks
concurrently.

See http://java.sun.com/javase/6/docs/api/java/lang/StringBuffer.html
Computer Science Notes
Chapter 8
Page 17 of 19

The StringBuilder class was introduced in JDK 1.5, and is more efficient when it will be accessed by a
single task.

See http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html
Section 8.4.1 Modifying Strings in the StringBuilder

The append method can be used to append information to the end of a StringBuilder object.

The delete and deleteCharAt methods can be used to delete a range of characters or a single
character, respectively, from a StringBuilder object.

The insert method can be used to insert characters into the middle of a StringBuilder object.

The replace method can be used to replace characters into the middle of a StringBuilder object.

The setCharAt method can be used to replace a single character in the middle of a
StringBuilder object.

The reverse method can be used to reverse the order of all characters in a StringBuilder object.
Section 8.4.2 The toString, capacity, length, setLength, and charAt Methods

The toString method returns a String object from the a StringBuilder object.

The capacity method returns the number of characters this StringBuilder object can store
without increasing its size.

The charAt method returns a single character from a StringBuilder object.

The length method returns the number of characters stored in a StringBuilder object.

The setLength sets a new length for a StringBuilder object (characters are either deleted or null
characters are appended).

The subString method returns a substring within a StringBuilder object.

The trimToSize method reduces the capacity of a StringBuilder object to the length of the
character array it stores.

Section 8.4.3 Problem: Ignoring Nonalphanumeric Charcters When Checking Palindromes

See www.cs.armstrong.edu/liang/intro7e/book/PalindromeIgnoreNonAlphanumeric.java
Section 8.5 Command Line Arguments

The main method always has an array of String objects for an input parameter…
public static void main(String[] args) {…}

The array of String objects is created and passed to main when the compiled Java program is run
from the command line with stuff typed after the name of the program…
java myProgram First Name hello 23

You have to write the code that processes the array of Strings.
Computer Science Notes
Chapter 8
Page 18 of 19
Section 8.5.1 Passing Strings to the main Method

To send strings to your program from the command line, simple type them after the name of the
program with a space between each string …
java myProgram First Name hello 23

You have to write the code that processes the array of Strings.
Section 8.5.2 Problem: Calculator

See www.cs.armstrong.edu/liang/intro7e/book/Calculator.java
Section 8.6 The File Class

The java.io.File class is used to obtain file properties and to delete and rename files.

The File class does not contain methods to read the contents of or write to a file.

See http://java.sun.com/javase/6/docs/api/java/io/File.html

See www.cs.armstrong.edu/liang/intro7e/book/TestFileClass.java
Section 8.7 File Input and Output

The File class does not contain methods to read the contents of or write to a file.

In order to perform I/O (input/output), you need to use objects that have appropriate methods for reading
and writing data.

The two classes this chapter uses are the Scanner class for reading and the PrintWriter class for
writing.

See http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

See http://java.sun.com/javase/6/docs/api/java/io/PrintWriter.html

There are 3 things you need to do in general in any programming language when dealing with disk files:
1. Open the file for reading or writing. In this chapter, opening a file will be accomplished by creating
an instance of a Scanner object or a PrintWriter object.
2. Read data from or write data to the file. In this chapter, we will read from a file using one of the
next* methods of the Scanner class, and we will write to a file using one of the print* methods of
the PrintWriter class.
3. Close the file. In this chapter, we will close a file by using the close() method of the Scanner and
PrintWriter classes.
Extra Java thing: any method that does I/O need to have throws Exception tacked on after the parameter list.
Computer Science Notes
Chapter 8
Page 19 of 19
Examples:
Text Input
//Create a File object.
java.io.File inputFile = new
java.io.File(inputData.txt);
Text Output
//Create a File object.
java.io.File outputFile = new
java.io.File(outputData.txt);
//Open the file for reading.
Scanner diskInput = new Scanner
(inputFile);
//Open the file for writing.
PrintWriter diskOutput = new
PrintWriter (outputFile);
//Read a line of text from the File.
String firstLine = diskInput.next();
//Write a line of text to the File.
diskOutput.println(Here is a line of
text written on the disk.);
//Close the File.
diskInput.close();
//Close the File.
diskOutput.close();
Section 8.7.1 Writing Data Using PrintWriter

See www.cs.armstrong.edu/liang/intro7e/book/WriteData.java

The print methods of the PrintWriter class write various types of information without including a
line feed.

The println methods of the PrintWriter class write various types of information with a line feed.

The printf method of the PrintWriter class performs formatted writes.

The close method of the PrintWriter class closes the file that was opened for writing.
Section 8.7.2 Reading Data Using Scanner

See www.cs.armstrong.edu/liang/intro7e/book/ReadData.java

The hasNext method of the Scanner class returns true if the Scanner has more data to be read.

The next method of the Scanner class returns the next token as a string delimited by whitespace.

The nextLine method of the Scanner class returns a line of text ending with the line separator.

The nextByte method of the Scanner class returns the next token as a byte.

The nextInt method of the Scanner class returns the next token as an int.

The nextShort method of the Scanner class returns the next token as a short.

The nextLong method of the Scanner class returns the next token as a long.

The nextDouble method of the Scanner class returns the next token as a double.

The nextFloat method of the Scanner class returns the next token as a float.
Section 8.7.3 How Does Scanner Work?

By skipping delimiters (i.e., whitespace), and the reading in a token up to the next delimiter.
Section 8.7.4 Problem: Replacing Text

See www.cs.armstrong.edu/liang/intro7e/book/ReplaceText.java
Section 8.8 (GUI) File Dialogs

See http://java.sun.com/javase/6/docs/api/javax/swing/JFileChooser.html

See www.cs.armstrong.edu/liang/intro7e/book/ReadFileUsingJFileChooser.java
Download