Java Console Input/Output The Basics

Similar documents
Java Console Input/Output The Basics. CGS 3416 Spring 2018

STUDENT LESSON A7 Simple I/O

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Peer Instruction 1. Elementary Programming

Programming with Java

PIC 20A Streams and I/O

Full file at

Reading Input from Text File

Chapter 2: Basic Elements of Java

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

First Java Program - Output to the Screen

I. Variables and Data Type week 3

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Simple Java Input/Output

Byte and Character Streams. Reading and Writing Console input and output

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Arithmetic and IO. 25 August 2017

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

ECE 122 Engineering Problem Solving with Java

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Objec&ves. Review. Standard Error Streams

CS 302: Introduction to Programming

Basics of Java Programming

Lecture 11.1 I/O Streams

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Java Input/Output. 11 April 2013 OSU CSE 1

double float char In a method: final typename variablename = expression ;

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

CS110: PROGRAMMING LANGUAGE I

Data Conversion & Scanner Class

Chapter 2: Data and Expressions

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Assignment 2.4: Loops

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Computational Expression

Chapter 2: Data and Expressions

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Computational Expression

Fundamentals of Programming Data Types & Methods

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Introduction Unit 4: Input, output and exceptions

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

FUNDAMENTAL DATA TYPES

Full file at

Dining philosophers (cont)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

What did we talk about last time? Examples switch statements

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

ing execution. That way, new results can be computed each time the Class The Scanner

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Basic Programming Elements

CS Programming I: File Input / Output

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input

JAVA Ch. 4. Variables and Constants Lawrenceville Press

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming. Java-Lecture 1

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

COMP String and Console I/O. Yi Hong May 18, 2015

CSCI 1103: File I/O, Scanner, PrintWriter

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS Programming I: File Input / Output

Introduction to Programming Using Java (98-388)

Sri Vidya College of Engineering & Technology Question Bank

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Midterm Examination (MTA)

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Experiment No: Group B_4

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Java Coding 3. Over & over again!

CS 302: Introduction to Programming in Java. Lectures 17&18. It s insanely hot. People desperately need some snowflakes

Lecture 8 " INPUT " Instructor: Craig Duckett

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

COMP 110/L Lecture 4. Kyle Dewey

Classes and Objects Part 1

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

Topics. The Development Process

Primitive Data Types: Intro

CS 251 Intermediate Programming Java I/O Streams

Getting started with Java

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

Programming Languages and Techniques (CIS120)

Lab 4. Java Concepts Lab 4

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Welcome to the Using Objects lab!

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS111: PROGRAMMING LANGUAGE II

Program Elements -- Introduction

We now start exploring some key elements of the Java programming language and ways of performing I/O

2: Basics of Java Programming

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Transcription:

Java Console Input/Output The Basics Lecture 3 COP 3252 Summer 2017 May 23, 2017

Console Output System.out out is a PrintStream object, a static data member of class System. This represents standard output Use this object to call functions print, println, and even printf print() converts parameter to a string (if not already one) and prints it out println() prints parameter, and also prints a newline after printf works like in C programming. Formatted string, followed by parameters to fill in the blanks

Sample Calls System.out.print("Hello, World"); System.out.println("Hello\n\nWorld"); // adds newline at end // no newline int feet = 6, inches = 3; System.out.printf("I am %d feet and %d inches tall\n", feet, inches); // just like printf in C

Concatenation If the + operator is used with at least one string operand, then the operation is string concatenation. Other types will be auto-converted to type string if needed System.out.println("The number of states in the U.S. is " + 50); int sides = 8; System.out.println("Number of sides on a stop sign = " + sides);

Formatting with printf When printing values with decimal precision it is often useful to be able to specify how many decimal places should be printed The C-style printf function provides an easy way to format decimal precision Format of printf calls: System.out.printf(format string, list of parameters); The format string is a string in quotes, with special format symbols inserted: %d specifies an integer %c specifies a character %s specifies a String %f specifies a floating point type Consider the format symbols to be fill-in-the-blanks spots in the format string. These are filled in with the list of parameters

printf Example int numstudents = 25; char lettergrade = A ; double gpa = 3.95; System.out.printf("There are %d students\n", numstudents); System.out.printf("Bobby s course grade was %c, and his GPA is %f\n", lettergrade, gpa); // The output from this example is: // There are 25 students // Bobby s course grade was A, and his GPA is 3.950000

printf Example To specify how many decimal places for the output of a floating point value, modify the %f symbol in this format: %.Nf // where N is the number of decimal places Example: double gpa = 3.275; double PI = 3.1415; System.out.printf("gpa = %.2f", gpa); System.out.printf("PI = %.3f", PI); // Output is: // gpa = 3.28 // PI = 3.142

Console Input Before Java version 1.5.0, console input was harder. Since 1.5.0, we have the Scanner class class Scanner is a text parser. Contains easy methods for grabbing different types of input System.in is an InputStream object that represents standard input To use Scanner to read from standard input: 1. Put the appropriate import statement at the top of the file: import java.util.scanner; 2. Create a Scanner object 3. Pass in System.in into the Scanner constructor, when creating the object

Example import java.util.scanner; // yadda yadda Scanner input = new Scanner(System.in); // now we can use the object to read data from // the keyboard (stdin). // Some sample calls: int x = input.nextint(); double y = input.nextdouble(); String s = input.next();

Reading different types of input Different data types require the use of different methods available in the Scanner class. These are available only for primitive types and Strings. For the following table, assume the Scanner object is called "in". Type byte short int long float double boolean String Reading in one variable byte b = in.nextbyte(); short s = in.nextshort(); int i = in.nextint(); long l = in.nextlong(); float f = in.nextfloat(); double d = in.nextdouble(); boolean bool = in.nextboolean(); String st = in.next();

Reading in chars The Scanner class does not have a method for reading in characters. However, there are several ways to read in a single character using the Scanner class. One such way is to read in a String and then grab the first character. String st = in.next(); char c = st.getcharat(0);

Checking for more input We usually want to take a peek into the Scanner buffer to see if an input token exists before we scan it in. This is to avoid exceptions/errors that occur when we try to read in data that doesn t exist or data of incompatible types. Just like the reading methods, the Scanner class provides methods that check the Scanner buffer for data of a particular type. All of these methods return a boolean, where true indicates data exists. These methods are very useful when we don t know the length of the input and have to read indefinitely from the Scanner.

Scanner methods to check for tokens Type byte short int long float double boolean String Checking for data of that type in.hasnextbyte(); in.hasnextshort(); in.hasnextint(); in.hasnextlong(); in.hasnextfloat(); in.hasnextdouble(); in.hasnextboolean(); in.hasnext();

Console Output using PrintWriter While it is perfectly acceptable to use System.out to print to the console, it was intended to be used by the system (printing logs, stack trace, tec.) We can use the PrintWriter class to print to the console (or any other file) as well. However, the print method of the PrintWriter class writes to the file byte by byte. To make it more convenient, we wrap the PrintWriter object within a BufferedWriter object. BufferedWriter bw = new BufferedWriter( new PrintWriter(System.out)); bw.write("hello world\n");