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 from the keyboard. Scanner is a class that knows how to read information from the terminal window.
Add knowledge to your program Bring that Scanner blueprint into your program with an import statement: import java.util.Scanner;
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
Your program talks to user System.out.println talks to user, but cannot get response System.out.println(“enter something”);
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();
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
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
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
Examples for pounds to kilograms Weight in pounds (int)Weight in kilograms
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
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
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();
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;
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");
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"); }
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 * = 20 * = 100 * 3
Our Program’s Steps 1.Find the length and width 2.Calculate the area 3.Print the area
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
Our Program’s Steps (continued) 1.1 Find the length 1.2 Find the width 2. Calculate the area 3. Print the area Prompt the user for the length Read the length Prompt the user for the width Read the width
Our Program’s Steps (continued) Prompt the user for the length Read the length Prompt the user for the width 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();
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;
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);
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." );
// 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); }
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.)
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); }