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.

Slides:



Advertisements
Similar presentations
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Advertisements

©2004 Brooks/Cole Chapter 3 Interactive Input. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
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!");
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Expressions, Data Conversion, and Input
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
COMP String and Console I/O Yi Hong May 18, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
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.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Introduction to Java The String class Input and Output.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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 prepared by Rose Williams, Binghamton University Console Input and Output.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Input/Output.
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
CSC 1051 – Data Structures and Algorithms I
Chapter 3 Java Input/Output.
Console Input and Output
Java Programming: From Problem Analysis to Program Design, 4e
Unit-2 Objects and Classes
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
Chapter 2 Part 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Introduction to Computing Using Java
Chapter 3 Input/Output.
The keyboard is the standard input device.
Primitive Types and Expressions
Presentation transcript:

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 a dot in the name of this object.)  One of the methods of System.out is println.  In this case, the argument you give println is the string you want to output.

Screen Output (cont’d)  Another method of System.out is print.  The difference between println and print is that println goes to a new line after printing the string and print does not.  For example: System.out.print(“One, two,”); System.out.print(“ buckle my shoe.”); System.out.println(“ Three, four,”); System.out.println(“ shut the door.”); will produce: One, two, buckle my shoe. Three, four, shut the door.

Keyboard Input  Keyboard input is done using the Scanner class, which is in the java.util package. ( util is short for utility.)  A package is simply a library of classes.  In order to make the java.util package available to your program, you must place the following line must be inserted at the beginning of your program: import java.util.*;

Keyboard Input (cont’d)  To handle keyboard input you can create an object of the class Scanner as follows: Scanner Scanner_Object_name = new Scanner(System.in); where Scanner_Object_name is any valid Java identifier.  For example, if keyboard is the identifer, we can write: Scanner keyboard = newScanner(System.in);

Keyboard Input (cont’d)  After this line appears you can use methods of the Scanner class with the object defined to read data that the user types in a the keyboard.  For example, the method nextInt reads one int value typed in at the keyboard.  The method actually reads in the next string of characters typed in, up to the next blank, and converts that string to an integer.

Methods in the Class Scanner  Scanner_Object_Name.next() Returns the String value consisting of the next keyboard characters up to, but not including, the first delimiter character (by default that is a blank).  Scanner_Object_Name.nextLine() Returns the rest of the current keyboard input line and returns the characters read as value of type String. Note that the line terminator ‘\n’ is read and discarded; it is not included in the string returned.

Methods in the Class Scanner (cont’d)  Scanner_Object_Name.nextInt() Returns the next value of type int that is typed in at the keyboard.  Scanner_Object_Name.nextLong() Returns the next value of type long that is typed in at the keyboard.  Scanner_Object_Name.nextByte() Returns the next value of type byte that is typed in at the keyboard.  Scanner_Object_Name.nextShort() Returns the next value of type short that is typed in at the keyboard.

Methods in the Class Scanner (cont’d)  Scanner_Object_Name.nextDouble() Returns the next value of type double that is typed in at the keyboard.  Scanner_Object_Name.nextFloat() Returns the next value of type float that is typed in at the keyboard.  Scanner_Object_Name.nextBoolean() Returns the next value of type boolean that is typed in at the keyboard. The values of true and false are entered as the strings “true” and “false”. Any combination of upper- and/or lower-case letters is allowed in spelling “true” and “false”.

Methods in the Class Scanner (cont’d)  Scanner_Object_Name.useDelimiter (Delimiter_Word) Makes the string Delimiter_Word the only delimiter used to separate input. That is, blanks and other white space will no longer be delimiters (unless they are part of Delimiter_Word ).