Professional Documents
Culture Documents
User Interaction
So far when we created a program there was no human interaction Our programs just simply showed one output In order for users to interact with our programs we need to use external classes
External Classes
External classes are ready made classes that are used to allow users to interact with a program
Two examples of external classes are; 1. The Scanner Class 2. The Keyboard Class
In order to use external classes you would need to let your program know that you would like to use them
In order to call ALL external classes you need to use the following code snippet
import java.util.*;
The scanner and keyboard classes are both used to allow users to interact with the program
Scanner Class
The first thing you need to do is create an instance of the Scanner class
Class
Constructor
Name
Scanner Inputs
String
double long
short
byte float boolean
short sh = s.nextShort();
byte b = s.nextByte(); float f = s.nextFloat(); boolean bo = s.nextBoolean();
import java.util.Scanner;
Examples
//Create an instance of the Scanner Scanner s = new Scanner(System.in); System.out.print("Enter your name : "); //Since the name is a String the String //has to be used String name = s.next();
System.out.println("How old are you ? "); //The age can be stored in a long long age = s.nextLong(); System.out.println("You are "+name+" and you are "+age+" years old."); } }
Keyboard Class
This class is also used to let users input data from the keyboard The user can input the same data types we spoke about when we used the scanner class
long
float double
char
boolean
Keyboard.readChar();
Keyboard.readBoolean(); Keyboard.readString();