Download presentation
Presentation is loading. Please wait.
1
BIT115: Introduction to Programming
Lecture 8 " INPUT " Instructor: Craig Duckett
2
Assignments Assignment 1 Revision due Lecture 10
Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment 3 at end of Lecture 10. IT IS RECOMMENDED YOU DO NOT TRY TO COMPLETE ASSIGNMENT 3 UNTIL AFTER LECTURE 11.
3
Assignment Dates (By Due Date)
Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22nd Assignment 2 (LECTURE 8) Section 1: Wednesday, January 31st Assignment 1 Revision (LECTURE 10) Section 1: Wednesday, February 7th Assignment 2 Revision (LECTURE 12) Section 1: Wednesday, February 14th Assignment 3 (LECTURE 13) Section 1: Wednesday, February 21st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5th Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12th The Fickle Finger of Fate
4
MID-TERM Mid-Term is NEXT LECTURE 9
Please be prompt, and bring a pencil … don’t worry, I’ll supply the paper
5
Lecture 10 and Going Forward
LECTURE 8 ENDS THE FIRST PHASE OF THE QUARTER --- WHAT THIS MEANS, AFTER THE MID-TERM: Less Theory, More Hands-On Work (Less means Less, not No) Less Hand-Holding, More Trial-and-Error Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (Occasionally) Aggravation Becker – Chapters 9.4, 9.5: INPUT System.in The Scanner Class
6
But First… The Quiz!
7
Input Input Output
8
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?
9
The Scanner Class Chapter 9.4, 9.5: Input
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;
10
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)
11
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.
12
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; userInput = keyboard.nextInt(); System.out.println("You entered " + userInput); } } Create Variable Get Input Do Something with Input ReadConsoleBasic.java
13
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
14
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
15
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 /
16
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!
17
A Closer Look: Basic_Keyboard_IO.java
18
A Look at Assignment 3 "The Maze"
You’ll have everything you need to successfully complete Assignment 3 "The Maze“ AFTER Lecture 12 and the discussion of Instance Variables. You can start working on the navigation and thing-putting portion of the maze before Lecture 12, but you need Instance Variables for the move-counting and printing portion.
19
A Closer Look: The ICE Exercises
else { System.out.println("You have not input a valid integer"); keyboard.nextLine(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.