Java
Download
1 / 12

Java - PowerPoint PPT Presentation


  • 137 Views
  • Uploaded on

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 output In order for users to interact with our programs we need to use external classes. External Classes .

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Java' - elda


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Java

Scanner Class

Keyboard Class


User interaction
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

  • External classes are ready made classes that are used to allow users to interact with a program

  • Two examples of external classes are;

  • The Scanner Class

  • The Keyboard Class


Using external classes
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


Scanner keyboard class
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


Scanner class
Scanner Class

  • The first thing you need to do is create an instance of the Scanner class

Class

Creates an instance of the class

Constructor

Name


Scanner inputs
Scanner Inputs

  • You could input many different data types;


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.");

}

}

Examples


Keyboard class
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



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.");

}

}



ad