correct the errors in the program.
/* Names.java */
import java.io.*;
/** The Names class provides a single function, main, that will
* perform various manipulations of the name John Fitzgerald Kennedy.
* This is a modification of the program on page 43 of Arnow and Weiss.
*/
class Names {
/** Performs various string operations on the name John Fitzgerald Kennedy.
* @param arg is not used.
*/
public static void main(String arg[]) {
String first = "John";
String middle = "Fitzgerald";
String last = "Kennedy";
String initials;
String firstInit, middleInit, lastInit;
firstInit = first.substring(0,1);
middleInit = middle.substring(0,1);
lastInit = last.substring(0,1);
initials = firstInit.concat(middleInit);
initials = initials.concat(lastInit);
System.out.println();
System.out.println(first + " " + middle + " " + last + " ");
System.out.println(initials);
System.out.println(last + ", " + first + " " + middle);
System.out.println(last + ", " + first + " " + middleInit +".");
System.out.println(first.toUpperCase() + " " + last.toUpperCase());
System.out.println(first + " equals john is " + first.equals("john"));
System.out.println(first + " equals john (ignoring case) is "
+ first.equalsIgnoreCase("john"));
System.out.println("The character at index 3 in " + middle + " is " +
middle.substring(3,4));
System.out.println("The index of \"gerald\" within " + middle + " is " +
middle.indexOf("gerald"));
System.out.println("The index of \"gerald\" within " + last + " is " +
last.indexOf("gerald"));
System.out.println();
}
}
class String的一些method:
public String substring(int beginIndex, int endIndex)
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:
"hamburger".substring(4, 8)
returns “urge”
"smiles".substring(1, 5)
returns “mile”
public String concat(String str)
Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
Examples:
"cares".concat("s")
returns “caress”
"to".concat("get").concat("her")
returns “together”
public String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
Examples:
"love".toUpperCase()
returns “LOVE”
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Examples:
String s = "John";
s.equals("john")
returns false
public boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
Examples:
String s = "John";
s.equalsIgnoreCase("john")
returns true
public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. The returned index is the smallest value k for which:
Examples
String s = "hello world!";
s.indexOf("wo")
returns 6