Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
 Finishing Chapter 1  This week : Chapter 2  Getting our home environment configured  Working on Assignment 1  See the website for more details! 
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2: Java Fundamentals Input and Output statements.
 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.
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
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.
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.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Chapter 2 Elementary Programming
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.
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 
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)
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
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
CSC 1051 – Data Structures and Algorithms I
Chapter 3 Java Input/Output.
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
Introduction to Classes and Methods
A+ Computer Science INPUT.
MSIS 655 Advanced Business Applications Programming
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Primitive Types and Expressions
Presentation transcript:

Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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); 2-2 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

println Versus print With println, the next output goes on a new line With print, the next output goes on the same line System.out.print(“Welcome "); System.out.print(“to our class "); System.out.println(“ICS 102"); System.out.println(“Computer Programming."); 2-3 Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Welcome to our class ICS 102 Computer Programming.

Console Input What if we want to let the user choose the values he wants to use ? We can prompt the user by console (keyboard) input

Console Input Import instruction Create Scanner object Read a first integer and assign it to variable a Read a second integer and assign it to variable b

Importing Packages and Classes Libraries in Java are called packages – A package is a collection of classes that is stored in a manner that makes it easily accessible to any program – In order to use a class that belongs to a package, the class must be brought into a program using an import statement 2-6 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Console Input Using the Scanner Class 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 – For example, in the following code the Scanner object is named scannerObject Scanner scannerObject = new Scanner(System.in); 2-8 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Method-Name Function nextBoolean() reads one boolean value (true/false) nextByte() reads one byte value nextShort() reads one short value nextInt() reads one int value nextLong() reads one long value nextFloat() reads one float value nextDouble() reads one double value next() reads one string of non-whitespace characters nextLine ()r eads an entire line of keyboard input 2-9 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Console Input

Console Input Using the Scanner Class Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method – Whitespace is any string of characters, such as blank spaces, tabs, and line breaks that print out as white space 2-11 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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 Hail University The value of word1 would be Hail, and the value of word2 would be University 2-12 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Console Input Using the Scanner Class The method next reads one string of non-whitespace characters delimited by whitespace The method nextLine reads an entire line of keyboard input – 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 2-13

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:");

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

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

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

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

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

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-20 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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-21 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

int n = keyboard.nextInt(); keyboard.nextLine(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); 2-22 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 1 of 3) 2-23 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 2 of 3) 2-24 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 3 of 3) 2-25 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Changing the Input Delimiter (Part 1 of 3) 2-27 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Changing the Input Delimiter (Part 2 of 3) 2-28 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Changing the Input Delimiter (Part 3 of 3) 2-29 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.