Documentos de Académico
Documentos de Profesional
Documentos de Cultura
s:
and
could be
, etc.
as follows:
Notice this method accepts any Object. Here, we will specifically use a
object.
The general syntax for usage of
is:
This method has three rules:
a. If alphabetically precedes coat room then it returns a negative .
b. If alphabetically comes after coat room it returns a positive .
c. If is alphabetically equal to coat room then it returns zero.
System.out.println( s.compareTo(coat room)); //
The reason we get a negative number in the example above is because T alphabetically
precedes c. Refer back to Lesson 13 and you will see that the ASCII code for capital T
is 84 and the ASCII code for little c is 99. The number 84 comes before 99 so we say
that The Dukes of Hazzard comes before (or alphabetically precedes) coat room.
There is another version of
17-2
Starting at index
Starting at index
17-3
int j = s.lastIndexOf(Haz);
System.out.println(j); //
int j = s.lastIndexOf(Haz, 11);
System.out.println(j); //
occurrences of
with
when this
contains the
; otherwise,
when this
contains
17-4
Scanner sc = new Scanner(Please, no more homework!);
A delimiter is a series of characters that separates the text presented to a
object
into separate tokens. The default delimiter is whitespace, so the separate tokens produced
by repeated application of the
method in the above example would be Please,,
no, more, and homework!. The
method allows a custom delimiter to
be set using regular expressions (see Appendix AC).
One of the key concepts here is that of the position of the
object as it scans its
way through the
. We will always think of this position as being
two
characters in the same way that a cursor in a word processor is never
a character;
rather it is always
two characters (or perhaps preceding the first character, or
just after the last character). As tokens are returned by
, etc., the position advances to
just after the last token returned.
Let us consider the following sequence of code where a
is passed to the
constructor of the
class as we illustrate the concept of position. We are also
going to introduce the
,
,
, and
methods
(all use regular expression arguments see Appendix AC). These methods will
; rather, their functions will be
:
Scanner sc = new Scanner("A string for testing scanner");
//The default delimiter of whitespace will be used.
System.out.println(sc.next( ));
//
System.out.println(sc.findInLine("ri"));
//
String ns = sc.next( ); //next( ) returns a String
System.out.println(ns);
//
sc.useDelimiter("r\\s+");
//
System.out.println(sc.next( ));
//
sc.skip(r\\s*test);
//
System.out.println(sc.next( ));
//
1. The position of a
object always moves
up. Likewise, searches (as with
e and
current position.
is returned.
17-5
3. In the
method the specified substring
indeed need to
follow
the current position. The r test sought after needs to immediately follow the current
position.
If the substring is not found, a
17-6
while(sc.hasNext( ))
{
//skip the stuff we want to get rid of
while(sc.hasNext("\\W|_"))
{
sc.skip("_*"); //skip underscores
if(sc.hasNext( ))
sc.skip("\\W*"); //skip non-word characters.Word characters are[a
//zA-Z_0-9]
}
if(sc.hasNext( ))
answer = answer + sc.next( );
}
System.out.println(answer); //prints
8 + 33 + 1,345 + 137
Exercise on Lesson 17
Project Encryption/Decryption
encrypt
String
decrypt
String
String
String
Tester