Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.

Similar presentations


Presentation on theme: "Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come."— Presentation transcript:

1 Scanner Pepper With credits to Dr. Siegfried

2 The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come from the keyboard. Scanner is a class that knows how to read information from the terminal window.

3 Add knowledge to your program Bring that Scanner blueprint into your program with an import statement: import java.util.Scanner;

4 Our Scanner Guy We need to create a Scanner guy (object instance) using the Scanner blueprint (class) and hold him in a Scanner box (variable) Scanner keyb = new Scanner(System.in); KEYB

5 Your program talks to user System.out.println talks to user, but cannot get response System.out.println(“enter something”);

6 Reading from the keyboard Once we create a Scanner guy (object instance), we can ask our Scanner variable (we called it keyb) to read an integer from the terminal window: variable = keyb.nextInt();

7 Ask before you read Scanner takes in typing, but does not ask for it. You do that: System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); Now, your variables are inside your program just as if you had set them to a specific number value 2 value1

8 Other things scanner can read You can read: –nextInt() –nextLong() –nextByte() –nextDouble() –next() – up to next whitespace (delimiter) –nextLine() – up to “\n” (end of line) –useDelimiter() Reading pointer –nextLine() moves your reading pointer down a line

9 Example – A program to convert pounds to kilograms Our program will convert a weight expressed in pounds into kilograms. –Our input is the weight in pounds. –Our output is the weight in kilograms –We also know that Kilograms = Pounds / 2.2

10 Examples for pounds to kilograms Weight in pounds (int)Weight in kilograms 00 -22-10 220100 225 102.27

11 Pounds to Kilograms Program (continued) Our program must: 1.Ask the user for the weight in pounds 2.Calculate the weight in kilograms 3.Print the weight in kilograms

12 Pounds to Kilograms Program (continued) Our program must: 1.Get the weight in pounds 2.Calculate the weight in kilograms 3.Print the weight in kilograms 1.1 Prompt the user for the weight in pounds 1.2Read the pounds

13 Pounds to Kilograms Program (continued) Our program must: 1.1 Prompt the user for the weight in pounds 1.2 Read the pounds 2. Calculate the weight in kilograms 3. Print the weight in kilograms System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt();

14 Pounds to Kilograms Program (continued) System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); 2. Calculate the weight in kilograms 3. Print the weight in kilograms double kg = lbs / 2.2;

15 Pounds to Kilograms Program (continued) System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); double kg = lbs / 2.2; 3. Print the weight in kilograms System.out.println("The weight is " + kg + " kilograms");

16 import java.util.Scanner; public class ConvertPounds { // Convert pounds to kilograms // Input - weight in pounds // Output - weight in kilograms public static void main(String[] args) { Scanner keyb = new Scanner(System.in); // Get the weight in pounds System.out.println ("What is the weight in pounds?"); double lbs = keyb.nextInt(); // Calculate and display the weight in // kilograms double kg = lbs / 2.2; System.out.println("The weight is " + kg + " kilograms"); }

17 Another Example – The Area of A Rectangle Our program will calculate the area of a rectangle. –Our input is the length and width. –Our output is the area. –We also know that Area = Length * Width 0 = 0 * 0 100 = 20 * 5 300 = 100 * 3

18 Our Program’s Steps 1.Find the length and width 2.Calculate the area 3.Print the area

19 Our Program’s Steps (continued) 1.Find the length and width 2.Calculate the area 3.Print the area 1.1 Find the length 1.2 Find the width

20 Our Program’s Steps (continued) 1.1 Find the length 1.2 Find the width 2. Calculate the area 3. Print the area 1.1.1 Prompt the user for the length 1.1.2 Read the length 1.2.1 Prompt the user for the width 1.1.2 Read the width

21 Our Program’s Steps (continued) 1.1.1 Prompt the user for the length 1.1.2 Read the length 1.2.1 Prompt the user for the width 1.1.2 Read the width 2. Calculate the area 3. Print the area System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble();

22 Our Program’s Steps (continued) System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); 2. Calculate the area 3. Print the area double area = length * width;

23 Our Program’s Steps (continued) System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); double area = length * width; 3. Print the area System.out.println("The area is " + area);

24 import java.util.Scanner; public class CalculateArea { // Calculates the area of a rectangle // Inputs - The length and width of the rectangle // Output - The area of the rectangle public static void main(String[] args) { Scanner keyb = new Scanner(System.in); // Print an explanatory message for the user System.out.println ("Given the width and length of a rectangle"); System.out.println ("this program calculates its area." );

25 // Get the inputs System.out.println("Enter the length?"); double length = keyb.nextDouble(); System.out.println("Enter the width?"); double width = keyb.nextDouble(); // Calculate and display the area double area = length * width; System.out.println("The area is " + area); }

26 TryScanner Play Tell the user to “Type an integer and then a word, and press Enter” Print it back to them with “You typed and.” Then, ask for a whole line and print it back. See that you need to be careful with the Enter keystroke. (Capture it with keyb.nextLine.)

27 Scanner Play solution import java.util.Scanner; public class ScannerPlay { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); System.out.println ("Type an integer and then a word, and press Enter"); int number1 = keyb.nextInt(); String word1 = keyb.next(); System.out.println("You typed " + number1 + " and " + word1 + "."); System.out.println("Type something else and Enter"); keyb.nextLine(); // skip a line String line1 = keyb.nextLine(); System.out.println("You typed " + line1); }


Download ppt "Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come."

Similar presentations


Ads by Google