User Input ICS2O.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
Hand Crafting your own program By Eric Davis for CS103.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
© 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.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
COMPUTER PARTS AND COMPONENTS INPUT DEVICES
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Parts of a Computer. Two Basic Components of a Computer System Hardware Parts of the Computer System you can physically touch Software Computer Instructions.
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
GOAL 1IDENTIFY THE MAIN ELEMENTS OF A COMPUTER SYSTEM. GOAL 2DESCRIBE INPUT DEVICES AND PROCESSING ACTIVITIES. GOAL 3EXPLAIN COMPUTER STORAGE MEDIA AND.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
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.
Chapter 2 More on Math More on Input
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
CSCI 161 – Introduction to Programming I William Killian
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 7 Top-Down Development
Console Input and Output
The Computer Work Stations
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Java Variables, Types, and Math Getting Started
Subprograms Intro & Procedures.
Subprograms Functions.
Truth tables: Ways to organize results of Boolean expressions.
Lec 4: while loop and do-while loop
Truth tables: Ways to organize results of Boolean expressions.
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
The keyboard is the standard input device.
Building Java Programs
User Input Keyboard input.
Random Numbers while loop
Input, Variables, and Mathematical Expressions
Input and Formatted Output (printf)
Optional Topic: User Input with Scanner
Presentation transcript:

User Input ICS2O

What is input? Data sent from the user into the computer for processing and output Many possible formats exist, to name a few: Sound (microphone) Graphical (image from a camera, scanner, etc.) Positional (from a mouse or touch screen) Text (from a keyboard) Input can come from many sources: Internet, keyboard, mouse, etc.

I.P.S.O. Concluded Input finishes the last of the four operations a computer can perform: Input (Console Input – today’s lesson, dynamic input – later) Processing (Calculations, Expressions) Storage (Variables) Output (Text, System.out.println(), System.out.print())

How Keyboard Input is Handled Different software handles keyboard input in different ways What happens in Microsoft Word when I tap the “w” key? A “w” shows up in the document What happens in a First Person Shooter like Call of Duty when I tap the “w” key? The character walks forward How/Why is there a difference? The operating system sends the keyboard input to the currently active software The software then decides whether to use the input, or ignore it

How does Java do it? Java is used in a lot of different user interfaces, sometimes that means a graphical interface with buttons, textboxes, etc. and other times that means the Console (as we will do today) In order to get input from the user in the Console using Java we need to make use of another library, the Scanner library. The following slides will go over the steps needed.

How does Java do it? Step 1: Import the library At the top of the program write: import java.util.Scanner; Step 2: Create a new object variable of type Scanner Inside main, define the variable as follows: Scanner input = new Scanner(System.in); The input variable, much like the Random object we saw previously can now be used. In this case to get input. Step 3: Create any needed variables to store read-in data This is done at the same locations we create all our variables

How does Java do it Step 4: Prompt the user This means instruct the user what data to enter using System.out.println() The message should be clear, e.g. “Name: “ or “Enter your age: “ Step 5: Read in the and store the data using the input variable To do this, the input object has two actions: input.nextLine()  Reads in the whole line of input input.next()  Reads in data until the first whitespace (space or tab) This is useful for getting multiple pieces of data from one input line E.g. userName = input.nextLine(); NOTE: All input using these commands comes to us as Strings, so if we want to store them in a different type of variable we must convert the data when reading it in. For Example: System.out.print(“Age: “); userAge = Integer.parseInt(input.nextLine());

Example Program import java.util.Scanner;  Step 1: import Scanner library public class Main { public static void main(String[] args) Scanner input = new Scanner(System.in);  Step 2: Create input object double recLength; double recWidth;  Step 3: Create storage variables double recArea; System.out.println(“Enter the rectangle length: “);  Step 4: Read in and store input recLength = Double.parseDouble(input.nextLine());  System.out.println(“Enter the rectangle width: “);  Step 4: Read in and store input recWidth = Double.parseDouble(input.nextLine());  recArea = recLength * recWidth; System.out.println(“The area of the rectangle is: “ + recArea); }