Input/Output.

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

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
 Finishing Chapter 1  This week : Chapter 2  Getting our home environment configured  Working on Assignment 1  See the website for more details! 
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
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.
Introduction to Programming
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.
String and Scanner CS 21a: Introduction to Computing I First Semester,
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
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.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
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.
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.
Introduction to programming in java
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Chapter 2 More on Math More on Input
Console Input and Output
TemperatureConversion
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
CSC 1051 – Data Structures and Algorithms I
Chapter 2 Screen Output Section 2.1
Console Input and Output
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
System.out.println for console output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
CSS 161: Fundamentals of Computing
Introduction to Classes and Methods
A+ Computer Science INPUT.
MSIS 655 Advanced Business Applications Programming
Introduction to Java Brief history of Java Sample Java Program
A+ Computer Science INPUT.
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Input/Output

System.out.println for console output System.out is an object that is part of the Java language println is a method invoked by the System.out object that can be used for console output The data to be output is given as an argument in parentheses A plus sign is used to connect more than one item Every invocation of println ends a line of output System.out.println("The answer is " + 42); Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-2 2

Copyright © 2008 Pearson Addison-Wesley. All rights reserved println Versus print Another method that can be invoked by the System.out object is print The print method is like println, except that it does not end a line With println, the next output goes on a new line With print, the next output goes on the same line Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-3 3

Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing simple keyboard input named the Scanner class In order to use the Scanner class, a program must include the following line near the start of the file: import java.util.Scanner; This statement tells Java to Make the Scanner class available to the program Find the Scanner class in a library of classes (i.e., Java package) named java.util Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-4 4

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Console Input The following line creates an object of the class Scanner and names the object keyboard : Scanner keyboard = new Scanner(System.in); Although a name like keyboard is often used, a Scanner object can be given any name Scanner input = new Scanner(System.in); Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-5 5

Console Input Using the Scanner Class Once a Scanner object has been created, a program can then use that object to perform keyboard input using methods of the Scanner class Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-6 6

Console Input import java.util.Scanner; Scanner input = new Scanner( System.in ); System.out.println( “Please type your name and press ‘return’ to continue.” ); //waits until the user types something and // presses return. input.nextLine();

Data I/O Another I/O example //this program echoes what you type //declare variables Scanner in = new Scanner( System.in ); //perform some i/o System.out.println( “Enter something & press ‘return’ to continue.” ); String what = in.nextLine(); System.out.print( “You typed: ” + what); //finish up in.close();

nextLine() Reads an entire line (including blanks and/or tabs) up to but not including return. The code, String line = keyboard.nextLine(); reads in an entire line and places the string that is read into the variable line

Copyright © 2008 Pearson Addison-Wesley. All rights reserved nextLine() The end of an input line is indicated by the escape sequence '\n' This is the character input when the Enter key is pressed 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 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) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-10 10

next() 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 Skips leading whitespace (blanks) including return. In effect, next() reads words including punctuation. Skips consecutive blanks (if any). Note: “blanks” include space, tab, and return. 2-11 11

Copyright © 2008 Pearson Addison-Wesley. All rights reserved next() 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 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-12 12

String input String s; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s = in.nextLine(); You enter: <sp>Oh!<tab>That is terrible!<sp><return> What is the value of s?

String input String s; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s = in.nextLine(); You enter: <sp>Oh!<tab>That is terrible!<sp><return> What is the value of s? s = “<sp>Oh!<tab>That is terrible!<sp>”

String input String s1, s2, s3; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s1 = in.next (); s2 = in.next(); s3 = in.nextLine(); You enter: <sp><sp>Oh!<tab>That is terrible!<sp><sp><return> What are the values of s1, s2, and s3?

String input String s1, s2, s3; Scanner in = new Scanner( System.in ); System.out.print( "Enter input: " ); s1 = in.next (); s2 = in.next(); s3 = in.nextLine(); You enter: <sp><sp>Oh!<tab>That is terrible!<sp><sp><return> What are the values of s1, s2, and s3? s1 = “Oh!” s2 = “That” s3 = “<sp>is terrible!<sp><sp>”

Data I/O Can we read other things (ints, doubles) besides strings?

Data I/O Can we read other things (ints, doubles) besides strings? Yes. //calculate F = M A //get values System.out.print( “enter mass: “ ); double mass = in.nextDouble(); System.out.print( “enter acceleration: “ ); double acc = in.nextDouble(); //calculate result double force = mass * acc; System.out.println( “force = “ + force );

Data I/O //calculate F = M A //get values System.out.print( “enter mass: “ ); double mass = in.nextDouble(); System.out.print( “enter acceleration: “ ); double acc = in.nextDouble(); //calculate result double force = mass * acc; System.out.println( “force = “ + force ); Can you convert this to use ints instead of doubles?

Data I/O //calculate F=MA //get values System.out.print( “enter mass: “ ); int mass = in.nextInt(); System.out.print( “enter acceleration: “ ); int acc = in.nextInt(); //calculate result int force = mass * acc; System.out.println( “force = “ + force );

Keyboard Input Demonstration (Part 1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-21 21

Keyboard Input Demonstration (Part 2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-22 22

Another Keyboard Input Demonstration (Part 1 of 3) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-23 23

Another Keyboard Input Demonstration (Part 2 of 3) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-24 24

Another Keyboard Input Demonstration (Part 3 of 3) Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-25 25

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 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-26 26

Pitfall: Dealing with the Line Terminator, '\n' 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? Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-27 27

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" Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-28 28

Pitfall: Dealing with the Line Terminator, '\n' 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') that was left over after reading the int Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-29 29

Pitfall: Dealing with the Line Terminator, '\n' Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); keyboard.nextLine(); 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? Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-30 30

Copyright © 2008 Pearson Addison-Wesley. All rights reserved The Empty String A string can have any number of characters, including zero characters "" 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 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2-31 31