String Processing
Specification (very partial) of java.lang.String class.
(1) < String > .length () -- < int >
``CS1 exams are fun! '' .length () -- 18
|
(2) < String > .indexOf (< String >) -- < int >
``frim fram sauce''.indexOf (``fr'') -- 0
``frim fram sauce''.indexOf (``fra'') -- 5
|
(3 a) < String > .substring (< int >) -- < String >
|
Returns the substring from the position specified by the parameter tot the end of the string.
``monday''.substring (3) -- ``day''
|
(3 b) < String > .substring (< int >, < int >)
-- < String >
|
Returns the substring from the position of the first parameter to the position one less than the sexond parameter.
``tired hand''.substring (2, 7) -- ``red h''
|
Note....
``cat'' == ``dog'' -- This only works
for numbers!!!
|
(4) < String > .equals (< String >) -- < boolean >
``cat''.equals (``dog'') -- false
``cat''.equals (``cat'') -- true
``cat''.equals (``Cat'') -- false
|
(5) < String > .equalsIgnoreCase (< String >)
|
(6) < String > .compareTo (< String >) -- < int >
|
Returns...
- 0 if the two strings are equal.
- Something negative if the receiving string is alphabetically less than the parameter.
- Something positive if the receiving string is alphabetically greater than the parameter.
``cat''.compareTo (``bat'') -- something positive
``bat''.compareTo (``bats'') -- something negative
|
Global procedural appliation architecture problem continued...
static private void computeTheAverage ()
{
int sum = 0;
for (int x = 0; x < nrElements; x + +)
{
sum = sum + a [x];
}
average = (double) sum / (double) nrElements;
static private void displayTheNumbersAboveAverage ()
{
for (int x = 0; int < nrElements; x = x + 1)
{
if (a [x] > average)
{
IO.println (a [x]);
}
}
}
|