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.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Introduction to Computer Programming Decisions If/Else Booleans.
Chapter 2: Java Fundamentals Input and Output statements.
Introduction to Computer Programming Decisions, Decisions: Boolean Expressions and Selection.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Java Variables and Expressions CSC160 Professor Pepper (presentation adapted from Dr. Siegfried)
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Introduction to Computer Programming Looping Around Loops II : Conditional Loops.
Introduction to Computer Programming CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper (presentation adapted from Dr. Siegfried)
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Computer Programming Lab(4).
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
User Input ICS2O.
CSC111 Quick Revision.
Input/Output.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 3 Java Input/Output.
Repetition.
Introduction to Classes and Methods
A+ Computer Science INPUT.
Java Language Basics.
Fundamentals 2.
Introduction to Computer Programming
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Object Oriented Programming
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Random Numbers while loop
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

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