Lecture 8 PowerPoint

advertisement
JULY
20
Lecture 5b
Instructor:
Craig Duckett
Input
Input
Output
Getting Input
 When creating and using computer programs, we can get input
in several different ways, either through a mouse, a keyboard, a
touch pad, a touch screen, cable (network, serial), wireless
mechanism, or a combination of several of these.
 For this class, we will be generating most of our input using the
keyboard.
 Now, how do we tell the java program that we want to use a
keyboard to get input from the user?
 How do we actually get the input from the user once the
program knows what to do with it?
Chapter 9.4, 9.5: Input
The Scanner Class
 To read input from the keyboard we use the Scanner class.
 Like Random, the Scanner class is defined in the Java Library
package called java.util, so we must add the following statement at
the top of our programs that require input from the user:
import java.util.*; // <-- I usually do this
or
import java.util.Scanner;
https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html
The Scanner Class
 Like print and println, Scanner objects work with Java’s
System class, but instead of .out it works with .in,
and is set up a bit differently
 To create a Scanner object:
Scanner keyboard = new Scanner(System.in);
NOTE:
Like any other object, keyboard here is just a name “made up” by the coder and can
be called anything instead—input, feedIine, keyIn, data, stuffComingFromTheUser,
etc.—although it should represent a word most apt to its purpose.
In this case I am using the name keyboard since it seems apt as I’ll be using the
keyboard to enter data (i.e., do the input)
Simple Input Logic
Create Variable Bucket to Hold Input
Get Input and Put in Variable Bucket
Do Something with Input in Variable Bucket
This is accomplished by the Scanner object using Scanner input methods
which we’ll look at in a moment. But first, let’s look at a simple input
program that demonstrates this simple input logic.
import java.util.*; // <-- Import library to use Scanner class
public class ReadConsoleBasic extends Object
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); // Scanner object
System.out.println("Please enter an integer:");
int userInput = 0;
 Create Variable
userInput = keyboard.nextInt();
 Get Input
System.out.println("You entered " + userInput);  Do Something with Input
}
}
ReadConsoleBasic.java
Scanner Input Methods for Integers
 These are int (integer) methods. There are also
Scanner methods available for floats, etc, which we'll
see later on in the quarter 
 nextInt()
 Assumes there is an int and does something with it
 hasNextInt()
 Checks to see if there is an int (boolean true or false)
 nextLine()
 Replaces the int in the keyboard buffer with a newline
character (Enter) so the program won't use the int again
Example: ReadConsole.java
import java.util.*; // Or import java.util.Scanner;
public class ReadConsole
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
// kb is the Scanner object
System.out.print("Enter an integer: ");
int a = kb.nextInt();
// nextInt() is a Scanner method
System.out.print("Enter an integer: ");
int b = kb.nextInt();
// nextInt() is a Scanner method
System.out.println(a + " * " + b + " = " + a * b);
}
}
A QUICK NOTE about Integer Division
Integer Division
 Division can be tricky.
In a Java program, what is the value of X = 1 / 2?
 You might think the answer is 0.5…
 But, that’s wrong.
 The answer is simply 0.
 Integer division will truncate any decimal remainder.
 If you are going to divide and need a decimal, then your
must use either the float or double types.
Let’s look at ReadConsole.java again but this time replacing multiplication *
with division /
Scanner Input Methods for Integers
 These are int (integer) methods. There are also
Scanner methods available for floats, etc, which we'll
see later on in the quarter 
 nextInt()
 Assumes there is an int and does something with it
 hasNextInt()
 Checks to see if there is an int (boolean true or false)
 nextLine()
 Replaces the int in the keyboard buffer with a newline
character (Enter) so the program won't use the int again
There are several demo programs to look at, so let’s look!
A Closer Look: Basic_Keyboard_IO.java
A Closer Look: The ICE Exercises
else
{
System.out.println("You have not input a valid integer");
keyboard.nextLine();
}
Lecture 5:
ICE 2
14
Download