Java Reference
In-Depth Information
E XERCISES : S TRINGS , S TRING B UFFERS , N UMBERS , AND B IG N UMBERS
Time to visit the example classes again and try out all these new ideas.
1. Edit the HelloWorld.java source file in your java4cobol directory with a
text editor. You'll start by deleting the code that experimented with flow
control statements. Remove the lines after this statement (but remember to
leave in the two curly braces at the end of the program):
// Print the contents of ErrorMsg's String data member directly.
System.out.println (myErrorMsg.msgText);
2. Add these Java expressions at the end of the previous statement (before the
last two curly braces). In this set of examples, a view of the output line is in-
serted after the println() statement so that the concept explored by the ex-
ample is easier to follow.
// Experiment with Java Strings.
// Construct a new String and initialize it, using its
// constructor.
// Point the object reference variable englishCaps to this new
// String.
String englishCaps = " ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
// Get the size of the String using the length() method.
System.out.println ("length() = " + englishCaps.length
());
...
// length() = 28 in this case
// Remove the leading and trailing spaces from the String.
// Point the reference variable englishCaps to this new String
englishCaps = englishCaps.trim ();
// Get the new size of the String using the length() method.
System.out.println ("length() = " +
englishCaps.length ());
...
// length() = 26 in this case
// Find the position of the character M.
System.out.println ("indexOf(M) = " +
englishCaps.indexOf("M"));
// Get the character at position 12.
Search WWH ::




Custom Search