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.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Introduction to Computer Programming Decisions If/Else Booleans.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Reading Information From the User Making your Programs Interactive.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Introduction to programming in java
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 2 Clarifications
CSC111 Quick Revision.
CS 160 Final Review.
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.
Lecture 14 Throwing Custom Exceptions
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
BIT115: Introduction to Programming
Unit-2 Objects and Classes
الكلية الجامعية للعلوم التطبيقية
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
Java so far Week 7.
Input, Variables, and Mathematical Expressions
Computer Science Club 1st November 2019.
Optional Topic: User Input with Scanner
Presentation transcript:

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

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.*;

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 The first thing you need to do is create an instance of the Scanner class ClassCreates an instance of the class Name Constructor

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

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

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

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

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

Which is better to use? Why?