Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© 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.
Introduction to C Programming
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Basic Input/Output and Variables Ethan Cerami New York
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Java Applications & Program Design
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Input, Output, and Processing
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
COMP String and Console I/O Yi Hong May 18, 2015.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
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.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Lecture 4 – Scanner & Style
Input/Output.
Chapter 2 More on Math More on Input
Console Input and Output
Chapter 2 Introduction to Java Applications
Console Input and Output
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
A+ Computer Science INPUT.
MSIS 655 Advanced Business Applications Programming
A+ Computer Science INPUT.
Introduction to Java Applications
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Presentation transcript:

Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

System.in & System.out Java objects connected to the console o System.in : keyboard (instance of InputStream ) o System.out : screen (instance of PrintWriter ) CSS 161: Fundamentals of Computing2 System.inSystem.out

Console Output System.out – Java PrintStream object associated with console (screen) print(), println() – Methods for writing to a PrintStream object – For now, we will use them only with System.out Syntax: – System.out.print(expression) – System.out.println([expression]) Semantics: – Evaluate expression, output its value – println : expression is optional (hence the square brackets []) output a carriage return / line feed (newline) at end of line CSS 161: Fundamentals of Computing3

Formatted output: printf Syntax: System.out.printf(format[,expression]*) Semantics: Use format string to output 0 or more expressions Examples: System.out.printf("Hello"); System.out.printf("Hello\n"); System.out.printf("%5.1f", 1.25); System.out.printf("%-5.1f", 1.25); System.out.printf("$.2f", 1.2); CSS 161: Fundamentals of Computing4

Format string specifiers CSS 161: Fundamentals of Computing5

Escape characters in format strings CSS 161: Fundamentals of Computing6

Console input: Scanner class First: not built-in, so have to import import java.util.Scanner; Next: declare & initialize an instance of the class Syntax: classname variable = new classname([args]) Semantics: Declare an instance of classname, initialize it (possibly with args), assign instance to variable Example: Scanner keyboard = new Scanner(System.in); Create an instance of the Scanner class Initialize it to accept input from the system console ( System.in ) Assign it to keyboard (NB: keyboard not a reserved word) CSS 161: Fundamentals of Computing7

Console Input Using the Scanner Class The method nextInt reads one int value typed in at the keyboard and assigns it to a variable: int numberOfPods = keyboard.nextInt(); The method nextDouble reads one double value typed in at the keyboard and assigns it to a variable: double d1 = keyboard.nextDouble(); Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method o Whitespace is any string of characters, such as blank spaces, tabs, and line breaks that print out as white space 2-8 CSS 161: Fundamentals of Computing

Console Input Using the Scanner Class The method next reads one String of non- whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line Given the code String word1 = keyboard.next(); String word2 = keyboard.next(); and the input line jelly beans The value of word1 would be “ jelly”, and the value of word2 would be “ beans” 2-9 CSS 161: Fundamentals of Computing

Console Input Using the Scanner Class The method nextLine reads an entire line of keyboard input The code, String line = keyboard.nextLine(); reads in an entire line and places the string that is read into the variable line The end of an input line is indicated by the escape sequence '\n' o This is the character input when the Enter key is pressed o On the screen it is indicated by the ending of one line and the beginning of the next line When nextLine reads a line of text, it reads the '\n' character, so the next reading of input begins on the next line o However, the '\n' does not become part of the string value returned (e.g., the string named by the variable line above does not end with the '\n' character) 2-10 CSS 161: Fundamentals of Computing

Keyboard Input Demonstration (Part 1 of 2) 2-11 CSS 161: Fundamentals of Computing

Keyboard Input Demonstration (Part 2 of 2) 2-12 CSS 161: Fundamentals of Computing

Another Keyboard Input Demonstration (Part 1 of 3) 2-13 CSS 161: Fundamentals of Computing

Another Keyboard Input Demonstration (Part 2 of 3) 2-14 CSS 161: Fundamentals of Computing

Another Keyboard Input Demonstration (Part 3 of 3) 2-15 CSS 161: Fundamentals of Computing

Pitfall: Dealing with the Line Terminator, '\n' The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off This can cause problems when combining it with different methods for reading from the keyboard such as nextInt Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, 2 Heads are better than 1 head. what are the values of n, s1, and s2 ? 2-16 CSS 161: Fundamentals of Computing

Pitfall: Dealing with the Line Terminator, '\n' Given the code and input on the previous slide n will be equal to "2", s1 will be equal to "", and s2 will be equal to "heads are better than" If the following results were desired instead n equal to "2", s1 equal to "heads are better than", and s2 equal to "1 head" then an extra invocation of nextLine would be needed to get rid of the end of line character ( '\n' ) 2-17 CSS 161: Fundamentals of Computing

Methods in the Class Scanner (Part 1 of 3) 2-18 CSS 161: Fundamentals of Computing

Methods in the Class Scanner (Part 2 of 3) 2-19 CSS 161: Fundamentals of Computing

Methods in the Class Scanner (Part 3 of 3) 2-20 CSS 161: Fundamentals of Computing

Java Documentation for Scanner CSS 161: Fundamentals of Computing 21

Programming Tip: Prompt for Input A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter the number of pods followed by"); System.out.println( "the number of peas in a pod:"); 2-22 CSS 161: Fundamentals of Computing

Programming Tip: Echo Input Always echo all input that a program receives from the keyboard In this way a user can check that he or she has entered the input correctly o Even though the input is automatically displayed as the user enters it, echoing the input may expose subtle errors (such as entering the letter "O" instead of a zero) 2-23 CSS 161: Fundamentals of Computing

Self-Service Checkout Line (Part 1 of 2) 2-24 CSS 161: Fundamentals of Computing

Self-Service Checkout Line (Part 2 of 2) 2-25 CSS 161: Fundamentals of Computing

The Empty String A string can have any number of characters, including zero characters o "" is the empty string When a program executes the nextLine method to read a line of text, and the user types nothing on the line but presses the Enter key, then the nextLine Method reads the empty string 2-26 CSS 161: Fundamentals of Computing

Other Input Delimiters The delimiters that separate keyboard input can be changed when using the Scanner class For example, the following code could be used to create a Scanner object and change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); After invocation of the useDelimiter method, "##" and not whitespace will be the only input delimiter for the input object keyboard CSS 161: Fundamentals of Computing

Changing the Input Delimiter (Part 1 of 3) 2-28 CSS 161: Fundamentals of Computing

Changing the Input Delimiter (Part 2 of 3) 2-29 CSS 161: Fundamentals of Computing

Changing the Input Delimiter (Part 3 of 3) 2-30 CSS 161: Fundamentals of Computing

Next Time(s) Wednesday: o Section 3.1: Branching mechanisms o Assignment 1 due (3:30pm) o Assignment 2 out Friday: o Lab (CSSSKL 161 A): UW1-120, 11:00-1:30 31CSS 161: Fundamentals of Computing