Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of...

27
Scanner Pepper With credits to Dr. Siegfried
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of...

Page 1: 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.

Scanner

PepperWith credits to Dr. Siegfried

Page 2: 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.

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.

Page 3: 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.

Add knowledge to your program

• Bring that Scanner blueprint into your program with an import statement:

import java.util.Scanner;

Page 4: 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.

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

Page 5: 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.

Your program talks to user

• System.out.println talks to user, but cannot get response

System.out.println(“enter something”);

Page 6: 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.

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();

Page 7: 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.

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 2value1

Page 8: 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.

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

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

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

Page 10: 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.

Examples for pounds to kilograms

Weight in pounds (int)Weight in kilograms

0 0

-22 -10

220 100

225 102.27

Page 11: 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.

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

Page 12: 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.

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 pounds1.2 Read the pounds

Page 13: 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.

Pounds to Kilograms Program (continued)

• Our program must:1.1 Prompt the user for the weight in pounds1.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();

Page 14: 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.

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;

Page 15: 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.

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");

Page 16: 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.

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"); }}

Page 17: 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.

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 * 0100 = 20 * 5300 = 100 * 3

Page 18: 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.

Our Program’s Steps

1. Find the length and width

2. Calculate the area

3. Print the area

Page 19: 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.

Our Program’s Steps (continued)

1. Find the length and width2. Calculate the area3. Print the area

1.1 Find the length1.2 Find the width

Page 20: 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.

Our Program’s Steps (continued)

1.1 Find the length1.2 Find the width 2. Calculate the area3. 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

Page 21: 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.

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 width2. Calculate the area3. Print the area

System.out.println("Enter the length?");double length = keyb.nextDouble(); System.out.println("Enter the width?");double width = keyb.nextDouble();

Page 22: 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.

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 area3. Print the area

double area = length * width;

Page 23: 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.

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);

Page 24: 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.

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." );

Page 25: 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.

// 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); }}

Page 26: 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.

Try Scanner Play

• Tell the user to “Type an integer and then a word, and press Enter”

• Print it back to them with “You typed <the first number they typed> and <the word they typed>.”

• 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.)

Page 27: 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.

Scanner Play solutionimport 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); }}