Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
User Input. Why user entry? When we assign values (age = 16), we are making a program static…it never changes. If we ask the user to enter the values,
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
1 Repetition structures Overview while statement for statement do while statement.
Relational Expression return Boolean results. operands must be of numerical types. == equal to != not equal to < less than > greater than
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
1 More on Assignment and Console Input Overview l Increment & Decrement Operators l Short-cut Operators l Casting l Math class l Console Input (TextIO.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
Reading Information From the User Making your Programs Interactive.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Computer Programming Lab(5).
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Saravanan.G.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Chapter 5: Preparing Java Programs 1 Chapter 5 Preparing Java Programs.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Files Chap. 10 Streams, Readers, Writers 1. 2 Problem In our array example, we entered the students' names and scores from the keyboard. In many situations.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
Mixing integer and floating point numbers in an arithmetic operation.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
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.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Another example of input import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader(
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
IO in java.
Introduction to programming in java
John Hurley CS201 Cal State LA
Strings and File I/O.
Interactive Standard Input/output
Computer Programming Methodology File Input
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Operators Laboratory /11/16.
Reading and Writing Text Files
Java so far Week 7.
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Using the Java Library API
class PrintOnetoTen { public static void main(String args[]) {
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
F II 2. Simple Java Programs Objectives
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides several static methods for reading particular types of data from standard input. public static String readString() public static String readWord() public static boolean readBoolean() public static char readChar() public static int readInt() public static long readLong() public static float readFloat() public static double readDouble()

import cs1.Keyboard; public class ReadKeyboardUsingKeyboardClass{ public static void main(String[] args) { int qty; double price, amountDue; System.out.print("Enter quantity purchased: "); qty = Keyboard.readInt(); System.out.print("Enter the unit price: "); price = Keyboard.readDouble(); amountDue = price * qty; System.out.println("Amount due: " + amountDue); } Keyboard Code Example

Install Keyboard – Method 1 Copy the cs1.jar file Paste it in the C:\jdk1.3\jre\lib\ext directory If the above doesn’t work by itself, then also paste it in C:\Program Files\JavaSoft\JRE\1.3\lib\ext

Install Keyboard – Method 2 Create a cs1 subdirectory to the directory containing your java programs Copy and compile Keyboard.java into the cs1 subdirectory Homework directory | +-Java source files | +- cs1 subdirectory | +- Keyboard.java +- Keyboard.class

Install Keyboard – Method 3 Copy Keyboard.java into the same directory as your programs. Remove the import statement from the code that calls Keyboard

Reading Input –JOptionPane import javax.swing.JOptionPane; public class ReadKeyboardViaJOptionPane{ public static void main(String[] args) { int qty; double price, amountDue; String temp; temp = JOptionPane.showInputDialog ("Please enter the quantity purchased: "); qty = Integer.parseInt(temp); temp = JOptionPane.showInputDialog ("Please enter the unit price: "); price = Double.parseDouble(temp); amountDue = price * qty; JOptionPane.showMessageDialog (null,"Amount due: " + amountDue); }

Reading Input – java.io import java.io.*; public class ReadKeyboardViaSystemIn{ public static void main(String[] args) throws IOException { int qty; double price, amountDue; BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); System.out.print("Please enter the quantity purchased: "); qty = Integer.parseInt(br.readLine()); System.out.print("Please enter the unit price: "); price = Double.parseDouble(br.readLine()); amountDue = price * qty; System.out.println("Amount due: " + amountDue); }