23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Additionally, Scanner objects can process <strong>in</strong>put l<strong>in</strong>e by l<strong>in</strong>e, ignor<strong>in</strong>g delimiters,<br />

<strong>and</strong> even look for patterns with<strong>in</strong> l<strong>in</strong>es while do<strong>in</strong>g so. The methods for process<strong>in</strong>g<br />

<strong>in</strong>put <strong>in</strong> this way <strong>in</strong>clude the follow<strong>in</strong>g:<br />

hasNextL<strong>in</strong>e(): Returns true if <strong>and</strong> only if the <strong>in</strong>put stream has another<br />

l<strong>in</strong>e of text.<br />

nextL<strong>in</strong>e(): Advances the <strong>in</strong>put past the current l<strong>in</strong>e end<strong>in</strong>g <strong>and</strong> returns<br />

the <strong>in</strong>put that was skipped.<br />

f<strong>in</strong>dInL<strong>in</strong>e(Str<strong>in</strong>g s): Attempts to f<strong>in</strong>d a str<strong>in</strong>g match<strong>in</strong>g the (regular<br />

expression) pattern s <strong>in</strong> the current l<strong>in</strong>e. If the pattern is found, it is returned <strong>and</strong> the<br />

scanner advances to the first character after this match. If the pattern is not found,<br />

the scanner returns null <strong>and</strong> doesn't advance.<br />

These methods can be used with those above, as well, as <strong>in</strong> the follow<strong>in</strong>g:<br />

Scanner <strong>in</strong>put = new Scanner(System.<strong>in</strong>);<br />

System.out.pr<strong>in</strong>t("Please enter an <strong>in</strong>teger: ");<br />

while (!<strong>in</strong>put.hasNextInt()) {<br />

<strong>in</strong>put. nextL<strong>in</strong>e();<br />

System.out.pr<strong>in</strong>t("That' s not an <strong>in</strong>teger; please enter<br />

an <strong>in</strong>teger: ");<br />

}<br />

<strong>in</strong>t i = <strong>in</strong>put.nextInt();<br />

1.7 An Example Program<br />

In this section, we describe a simple example <strong>Java</strong> program that illustrates many of<br />

the constructs def<strong>in</strong>ed above. Our example consists of two classes, one,<br />

CreditCard, that def<strong>in</strong>es credit card objects, <strong>and</strong> another, Test, that tests the<br />

functionality of CreditCard class. The credit card objects def<strong>in</strong>ed by the<br />

CreditCard class are simplified versions of traditional credit cards. They have<br />

identify<strong>in</strong>g numbers, identify<strong>in</strong>g <strong>in</strong>formation about their owners <strong>and</strong> their issu<strong>in</strong>g<br />

bank, <strong>and</strong> <strong>in</strong>formation about their current balance <strong>and</strong> credit limit. They do not charge<br />

<strong>in</strong>terest or late payments, however, but they do restrict charges that would cause a<br />

card's balance to go over its spend<strong>in</strong>g limit.<br />

The CreditCard Class<br />

68

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!