File I/O Array Basics For-each loop

Similar documents
Instructor: Dr. Joanne Selinski TA: Steven Pearlman

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS 251 Intermediate Programming Java I/O Streams

Computational Expression

Text User Interfaces. Keyboard IO plus

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

A token is a sequence of characters not including any whitespace.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Recitation: Loop Jul 7, 2008

Reading Input from Text File

Input. Scanner keyboard = new Scanner(System.in); String name;

Full file at

Java Input/Output. 11 April 2013 OSU CSE 1

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

Java API: Math, Wrapper Classes, StringBuffer Random Data Quiz2

Object-Oriented Programming in Java

Chapter 4: Loops and Files

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Chapter 4: Loops and Files

CIS 110: Introduction to Computer Programming

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

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

CSCI 1103: File I/O, Scanner, PrintWriter

CSCI 1103: File I/O, Scanner, PrintWriter

HST 952. Computing for Biomedical Scientists Lecture 8

CS Programming I: File Input / Output

Computer Science is...

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CS Programming I: File Input / Output

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

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

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

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

Simple Java Input/Output

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Programming Languages and Techniques (CIS120)

Dining philosophers (cont)

Programming with Java

Data Types and the while Statement

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

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

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

Using Java Classes Fall 2018 Margaret Reid-Miller

Fundamental Java Syntax Summary Sheet for CISC101, Spring Java is Case - Sensitive!

java_help_index 08/14/18 07:14:34 walton 1 of 1

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Reading Text Files. 1 Reading from text files: Scanner

Chapter 2: Data and Expressions

ECE 122 Engineering Problem Solving with Java

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

Section 2: Introduction to Java. Historical note

Input-Output and Exception Handling

CS 180. Recitation 8 / {30, 31} / 2007

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Full file at

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

PIC 20A Streams and I/O

Computational Expression

FILE I/O. CS302 Introduction to Programming University of Wisconsin Madison Lecture 27. By Matthew Bernstein

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

13 th Windsor Regional Secondary School Computer Programming Competition

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Data Conversion & Scanner Class

Pace University. Fundamental Concepts of CS121 1

Fundamentals of Programming Data Types & Methods

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Chapter. Let's explore some other fundamental programming concepts

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

File I/O and Exceptions

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

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Section 2.2 Your First Program in Java: Printing a Line of Text

Programming Languages and Techniques (CIS120)

Streams and File I/O

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

First Java Program - Output to the Screen

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Lesson 3: Accepting User Input and Using Different Methods for Output

CS 112 Introduction to Programming

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

Lecture 8 " INPUT " Instructor: Craig Duckett

File Practice Problems

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

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Experiment No: Group B_4

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

COMP 202 Java in one week

Sorting/Searching and File I/O

A variable is a name for a location in memory A variable must be declared

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Transcription:

File I/O Array Basics For-each loop 178

Recap Use the Java API to look-up classes/method details: Math, Character, String, StringBuffer, Random, etc. The Random class gives us several ways to generate random data, but we often need to apply transformations to get what we need Review posted RandomStuff solution 179

Text Files for Input/Output text files are simply character streams (sequences) standard streams: System.in, System.out, System.err (also for output, used for error messages usually) import java.io.*; // I/O package needed for file classes use FileReader (input) and PrintWriter (output) classes for character data IOException must be caught or thrown by every method that indirectly or directly uses an IO class (particularly for input): public static void main(string[] args) throws IOException EXAMPLES: code/filesdemo.java [There are other types of files that we won't be using in this course.] 180

Writing to Files Declare and initialize an object linked to the external filename: PrintWriter outfile = new PrintWriter("outfile.txt"); Use print(), println() and printf() to write data to the file, just like we did with System.out: outfile.print("writing to this file"); Save the data permanently to your file system when done: outfile.close(); // very important to save data permanently use String literal or variable with external file name to create will overwrite existing contents if file already exists, will create new file otherwise must close file to save permanently! 181

Reading from files reading from a file: Scanner filescan = new Scanner(new FileReader("infile.txt")); // proceed as usual: filescan.next(), nextint(), nextdouble(), etc. infile.close(); // when finished reading from it use String literal or variable with external file name to initialize file must already exist, otherwise an exception is thrown reading to end of input: while (scan.hasnext()) // scan can be keyboard or string or file true if there is more input false when the end of input is reached type ^d (unix, Mac) or ^z (PC, jgrasp) to denote end of keyboard input, or right-click in jgrasp and choose "Close Input Stream" 182

Review: Scanner class for input import java.util.*; Create object of class, init w/system.in for keyboard input, or with a String, or with a FileReader Methods for various data types, separated by whitespace (space, tab, newline) next() (String, stopping at first whitespace) nextint() (int) nextdouble() (double) next().charat(0) (char) nextline() (String, entire line to end including whitespace) hasnext() or hasnextline() is true if not at end of input, false if at end (good for controlling loops) 183

More Method Docs We can add more tags depending on what else affects the method, include the @throws tag for methods that throw things like the IOException /** General description of method goes on the first line. @param first what the first parameter is for, plus assumptions @param second how the second param is used, etc. @return what gets returns if not void, otherwise omit @throws SomeException explain any declared exceptions */ Remember: checkstyle requires the description to end in a period 184

Pig-Latin Revisited Problem Statement: We want a program that can convert a text document from English to Pig-Latin (simplified version). This is some pig latin. Histay ishay omesay igpay atinlay. (simplified form) Subroutines we need to write: String convertword(string orig) String convertline(string line) void convertfile(string origfile, String newfile) 185

Pig-Latin Pseudocode convertfile(string origfile, String newfile) Initialize input file Initialize output file For every line in the input file Write the conversion to the output file Write an end of line character to the output convertline(orig) Set result to For every word in orig: Add convertword(word) to result Add space to result Return result 189

Pig-Latin completed Write a full pig-latin program using these subroutines: String convertword(string orig) (adapt code from 9/25) String convertline(string line) void convertfile(string origfile, String newfile) Your main method should get the name of the input file. The output file should be named Pig + the input filename. If extra time, deal with capital letters correctly deal with punctuation correctly Submit on Blackboard as c10 190

I/O Recap File I/O requires an extra step to initialize and one to save, and declaring an exception File input uses a Scanner as usual File output using print, println, printf as usual 191

Arrays allow one variable to hold multiple values all values must be of the same base type integer indices are used to access individual values, called array elements [] are used to denote indices indices begin at 0 always we must declare a size (capacity) to create arrays are reference objects in java.length tells us the declared size of any array variable 194

Array examples Declare variable: ra int[] ra; double xray[]; Allocate memory: ra = new int[20]; int size = 6; xray = new double[size]; Access element: ra[0] = 23; xray[3] = -23.492; 0 1 2 3 4 5... 17 18 19 0 1 2 3 4 5 xray 195

Array Initializations primitive base types are initialized to 0 reference (object) base types are initialized to null can use an initializer list to declare & initialize if base type is primitive or String: int hw[] = {40, 30, 25, 35, 42, 47}; can use a loop to initialize various ways: for (int i=0; i < hw.length; i++) hw[i] = i*2; 196

Array Practice Piazza polls: @210 Array Init1 @211 Array Init2 @212 Array Init3 @208 Array Init4 197

Midterm Reminders In class Wednesday Bring writing implements no other tools allowed Bring J-card for identification (required!) Spread out as soon as you get to class 198

Midterm! 199