|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
tip: When writing a loop, first figure out the body — that is, what action do you want to do, over and over and over?
tip: When writing the condition for a loop, explain to yourself why you are sure this loop will eventually finish. Hey — now that I think about it, these above loops aren't guaranteed to finish; sometimes they might run forever: an infinite loop. When, exactly, does that happen? How to fix it? (Or should it even be fixed, in this case?)
tip (BlueJ): You recognize an infinite loop by the fact that your IDE stops responding. In BlueJ, you'll see the progress bar (underneath the compile button) churning away like a red and white barber pole (see image on left). You can halt your program by right-clicking on that progress bar (see image on right):
Practice:
I want a method for BankAccounts
which keeps making a $5 deposit until
the balance is more than $500.
(Not bad!)
Question: are you sure this loop will always finish?
Task: Find the most recent xkcd cartoon. Or at least, use repeated doubling to find an index which is too big (in a methodical way, applicable to any situation):
public static int findXkcdTooBig() throws java.net.MalformedURLException, java.io.IOException { final String XKCD_HOME = "http://xkcd.com/"; java.net.URL theURLToTry; java.util.Scanner s; int nextTry = 1; theURLToTry = new java.net.URL( XKCD_HOME + nextTry ); s = new java.util.Scanner( theURLToTry.openStream() ); System.out.println( " I read " + s.next() + " from XKCD # " + nextTry ); while (s.hasNext()) { nextTry = 2*nextTry; theURLToTry = new java.net.URL( XKCD_HOME + nextTry ); s = new java.util.Scanner( theURLToTry.openStream() ); System.out.println( " I read " + s.next() + " from XKCD # " + nextTry ); } return nextTry; } |
public static int findXkcdTooBig() throws java.net.MalformedURLException, java.io.IOException { final String XKCD_HOME = "http://xkcd.com/"; int nextTry = 1; java.net.URL theURLToTry; java.util.Scanner s; do { nextTry = 2*nextTry; theURLToTry = new java.net.URL( XKCD_HOME + nextTry ); s = new java.util.Scanner( theURLToTry.openStream() ); System.out.println( " I read " + s.next() + " from XKCD # " + nextTry ); } while ( s.hasNext() ); return nextTry; } |
What does the following method return?
/** Announce a countdown, starting at 10. * @return A countdown starting at 10. * That is, "10...9...8... [etc] 2...1...Liftoff!" */ public String countdown() { String chantSoFar = ""; /* A local variable, to accumulate the answer. */ int t = 10; /* The next number to count down. */ while (t >= 0) { chantSoFar = chantSoFar + (t + "..."); t = t-1; } return (chantSoFar + "Liftoff!"); } |
style tip: Don't just make the parameter named t and then assign to it (but mention that approach). Keeping the parameter untouched segues well into the next problem.
Another example of a while loop:
We saw earler, that if we have a source of characters (like the keyboard),
we can make a java.util.Scanner who can take those individual
characters and turn them into numbers (ints or doubles) or strings for us,
with the method nextInt(), nextDouble(),
next() (which returns the next word, as a string).
There are other useful
Scanner
methods: nextLine() which returns the entire
next line (a String),
as well as
a boolean method hasNext(), which
determines whether the source of characters has run dry.
/** Open up a URL, and read the input. * As a side-effect (for demonstration only), print each line to the console. * @return The number of lines in a given URL. */ public static int countLines() throws java.net.MalformedURLException, java.io.IOException { String sourcePage = "http://www.radford.edu/itec120/2008spring/"; // Open sourcePage for input: (Create a URL object based on sourcePage, open that URL as a stream-of-characters, // and create a Scanner who will group that stream's characters into Strings etc.) java.util.Scanner src = new java.util.Scanner( new java.net.URL( sourcePage ).openStream() ); int linesSoFar = 0; // Keep track of how many lines we've seen. while ( src.hasNext() ) { src.nextLine(); // Have the scanner consume the next line. We ignore its return value. linesSoFar = linesSoFar + 1; } return linesSoFar; } |
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Oct.24 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |