Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.

Similar presentations


Presentation on theme: "Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one."— Presentation transcript:

1 Java Scanner Class Keyboard Class

2 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

3 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

4 Using External Classes 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.*;

5 Scanner & Keyboard Class The scanner and keyboard classes are both used to allow users to interact with the program They are used to ask users for an input The input is normally done using the actual keyboard

6 Scanner Class The first thing you need to do is create an instance of the Scanner class ClassCreates an instance of the class Name Constructor

7 Scanner Inputs You could input many different data types; Data TypeMethod String String str = s.next(); double double d = s.nextDouble(); long long l = s.nextLong(); short short sh = s.nextShort(); byte byte b = s.nextByte(); float float f = s.nextFloat(); boolean boolean bo = s.nextBoolean();

8 Examples import java.util.Scanner; class test { public static void main (String args[]){ //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."); }

9 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

10 Using the Keyboard Class Data TypeMethod int Keyboard.readInt(); byte Keyboard.readByte(); short Keyboard.readShort(); long Keyboard.readLong(); float Keyboard.readFloat(); double Keyboard.readDouble(); char Keyboard.readChar(); boolean Keyboard.readBoolean(); String Keyboard.readString();

11 class test { public static void main (String args[]){ System.out.print("Enter your name : "); String name = Keyboard.readString(); System.out.println("How old are you ? "); long age = Keyboard.readLong(); System.out.println("You are "+name+" and you are "+age+" years old."); }

12 Which is better to use? Why?


Download ppt "Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one."

Similar presentations


Ads by Google