Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.

Similar presentations


Presentation on theme: "Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder."— Presentation transcript:

1 Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder

2 2 Assignment 2 (LECTURE 8) GRADED! RETURNED! Wednesday, April 29 Assignment 1 Revision (LECTURE 10) Wednesday, May 6 Assignment 2 Revision (LECTURE 12) Wednesday, May 13 Assignment 3 (LECTURE 13) Monday, May 18 Assignment 3 Revision (LECTURE 16) Monday, June 1 Assignment 4 (LECTURE 20) Wednesday, June 10 Assignment 4 Revision (LECTURE 21) Monday, June 15 Assignment Dates DUE NEXT

3 Lecture 10 Announcements TODAY BEGINS THE SECOND HALF OF THE QUARTER --- WHAT THIS MEANS: Less Theory, More Hands-On Work Less Theory, More Hands-On Work (Less means Less, not No) Less Hand-Holding, More Trial-and-Error Less Hand-Holding, More Trial-and-Error Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (Occasional) Aggravation Less Explanation, More Research & Investigation, More Poking Around For Code, More “Googling It” and More (Occasional) Aggravation ----------------------------------------------------------------------- ----------------------------------------------------------------------- Becker – Chapters 9.4, 9.5: Input System.in The Scanner Class

4 Lecture 10 Announcements What We Will Be Going Over Today Becker – Chapters 9.4, 9.5: Input System.in The Scanner Class

5 But First… The Quiz!

6 Chapter 9.4, 9.5: Input The Scanner Class To read input from the keyboard we can use the Scanner class. Like Random, the Scanner class is defined in java.util, so again we will use the following statement at the top of our programs: import java.util.*; or import java.util.Scanner;

7 The Scanner Class Scanner objects work with System.in To create a Scanner object: Scanner keyboard = new Scanner(System.in); NOTE: Like any other object, keyboard here is a name “made up” by the coder and can be called anything—input, feedIine, keyIn, data, stuffComingFromTheUser, etc.—although it should represent a word most apt to its purpose. In this case we are using keyboard since it seems most apt.

8 Example: ReadConsole.java import java.util.Scanner; // Or import java.util.*; public class ReadConsole { public static void main(String[] args) { Scanner cin = new Scanner(System.in); System.out.print("Enter an integer: "); int a = cin.nextInt(); System.out.print("Enter an integer: "); int b = cin.nextInt(); System.out.println(a + " * " + b + " = " + a * b); } } A NOTE about Integer Division

9 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.

10 import java.util.Scanner; // Or import java.util.*; public class ReadConsoleChecked { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a = 0; while (true) // <-- A new kind of while loop { System.out.print("Enter an integer: "); if (cin.hasNextInt()) // Checks to see whether an int has been typed in keyboard { a = cin.nextInt(); cin.nextLine(); // newline flush break; } else { String next = cin.nextLine(); // newline flush System.out.println(next + " is not an integer such as 10 or -3."); } } int b = 0; while (true) // <-- A new kind of while loop { System.out.print("Enter an integer: "); if (cin.hasNextInt()) { b = cin.nextInt(); cin.nextLine(); // newline flush break; } else { String next = cin.nextLine(); // newline flush System.out.println(next + " is not an integer such as 10 or -3."); } } System.out.println(a + " * " + b + " = " + a * b); } }

11 A Closer Look: Basic_Keyboard_IO.java

12 A Closer Look: The ICE Exercises else { System.out.println("You have not input a valid integer"); keyboard.nextLine(); }


Download ppt "Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder."

Similar presentations


Ads by Google