Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Input ICS2O.

Similar presentations


Presentation on theme: "User Input ICS2O."— Presentation transcript:

1 User Input ICS2O

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

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

4 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

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

6 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

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

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


Download ppt "User Input ICS2O."

Similar presentations


Ads by Google