Slide Set Three - Basic Data Input and Output Using Scanner
Slide Set Three - Basic Data Input and Output Using Scanner
INFO6066
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
• Think of the import statement as your library card which allows you to
access any class in the Java library.
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
• To use it, you first have to ask Java to create a Scanner object in your program.
• We have to give this object a name or “reference”, and we often use the variable
name “input” to name the Scanner object (although you can name it anything you
want, “input” is a nice short name to type, as opposed to “keyboardReader”).
• Now that you have created the Scanner object, you “reference it” (refer to it) by using
its name…input System.in is the “argument” we pass to the
“constructor method”of the Scanner class.
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
//call nextInt() method to read the keyboard buffer. It will return any digit
characters that it finds in the buffer BUT NOT THE EOL!
int userInput = input.nextInt( );
INFO6066
Software and Information Systems Testing
//call nextDouble() method to read the keyboard buffer. It will return any
digits or a decimal point, BUT NOT THE EOL!
double userInput = input.nextDouble( );
INFO6066
Software and Information Systems Testing
• Trying to read in text output using nextLine() after reading in numeric output using
nextInt() or nextDouble() or any of the other methods for reading in numeric input
can cause an annoying logic error if you are not careful.
• Remember that the nextLine() method will “flush” the keyboard buffer and
completely empty it of all characters, including the EOL or new line character (the
ENTER key).
• The methods for reading numeric input are also “rude house guests” as they only
read in numeric characters, or the period character if a decimal point is used.
• When the numeric reading methods encounter the EOL character, they stop
reading, but they also leave the EOL character behind in the buffer! The buffer is
not “flushed” clean.
INFO6066
Software and Information Systems Testing
Warning, cont’d…
• All of the numeric reading methods are rude house guests (just like the next()
method), who do not “flush” after using your bathroom.
• This EOL character that is left in the buffer by the numeric reading methods will
cause problems if you then attempt to use nextLine() to read some text input from
the user.
• Again, the nextLine() method is triggered by the presence of any characters in the
keyboard buffer. If there is an EOL character left in there from a previous read of
numeric input, then nextLine() will immediately run, before the user has a chance
to input any text data that you might be asking for.
• Again, you need to “flush” the buffer after you read in any numeric input, if you
need to use nextLine() after the numeric read .
• To do a buffer flush, you just enter this line: input.nextLine();
• There is a YouTube video on this topic by Professor Graham Mansfield , at
http://www.youtube.com/watch?v=bnNHKnHMbt4
INFO6066
Software and Information Systems Testing
• Step 3: create a variable that will hold the value that the Scanner
method will return from the keyboard buffer.
INFO6066
Software and Information Systems Testing
Recap…when do I flush?
• If you use the next() method, or do a numeric read method such as nextInt() or
nextDouble(), remember that they leave the EOL character behind in the buffer.
• If you then ask the user to enter some text that you plan to read using nextLine()
(i.e. their full name, with spaces), the presence of the EOL character will cause the
nextLine() method to fire right away, before the user has a chance to type in
anything.
• Before you prompt the user to enter the text, do a “buffer flush” by using this line:
input.nextLine(); //flushes the buffer clean
• Once the buffer is flushed, you can ask the user to enter their text.
INFO6066
Software and Information Systems Testing
Homework
• Read the rest of Chapter Two:
• Please engage with the exercises.
• Answer the questions.
• Complete the coding examples.
INFO6066