Program 6 Any questions?. System.in Does the opposite of System.out.

Slides:



Advertisements
Similar presentations
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Advertisements

10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3b Standard Input and Output Sample Development.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Two Ways to Store Data in a File Text format Binary format.
Looping While-continue.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Array Processing.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
File IO Basics By Dan Fleck Coming up: Data Streams.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Chapter 4: Control Structures II
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
For Friday Finish reading chapter 9 WebCT quiz 17.
Introduction to programming in java
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Chapter 7 Part 1 Edited by JJ Shepherd
Week 14 - Wednesday CS 121.
Chapter 5: Control Structures II
INPUT STATEMENTS GC 201.
For Monday Read WebCT quiz 18.
Control Statement Examples
Yong Choi School of Business CSU, Bakersfield
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
Reading and Writing Text Files
For Wednesday No new reading No quiz.
Let’s all Repeat Together
File Input and Output.
I/O Exceptions & Working with Files
Presentation transcript:

Program 6 Any questions?

System.in Does the opposite of System.out

Reading Lines of text Single words Integers Doubles Etc.

Practice Problem Write Java code to ask for a student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. The letter grade is to be calculated as follows: 90 and aboveA 80-89B 70-79C 60-69D below 60F

Switch Practice A program is required to read a customer’s name, a purchase amount and a tax code. The tax has been validated and will be one of the following: 0tax exempt (0%) 1state sales tax only (3%) 2federal and state sales tax (5%) 3special sales tax (7%) The program must then compute the sales tax and the total amount due and print the customer’s name, purchase amount, sales tax and total amount due.

Sentinel-Controlled loops Initialize variable to be tested Test variable against sentinel value -Process variable as needed -Update variable to be tested

Practice Design and write a program that will prompt for, receive, and total a collection of payroll amounts entered by the user until a sentinel amount of -99 is entered. After the sentinel has been entered, display the total payroll amount on the screen.

Problem 3 Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be

Problem 4 Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read. Do not count –999 as one of the integers to consider.

Working with Files Input is very straightforward. We just connect our Scanner to a File object instead of System.in or a String Do have to deal with exceptions Often just declare that we throw the exceptions

throws Clause Just an announcement that a particular method may throw a particular exception Used to help the programmers that call the method (more on exceptions in chapter 11)

Writing Files Writer classes Each classes has a purpose –FileWriter – connects to a file –BufferedWriter – makes output more efficient –PrintWriter – lets you use print and println

Using the FileWriters FileWriter fw = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); out.println(“the first line”);

Alternative PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter(fileName))); out.println(“the first line”);

Notes Using files always potentially throws IOExceptions. Sometimes we can handle these gracefully. Sometimes we need to allow the program to report an error and end.

File Practice Write code to read a file of integers and write a new file with one integer per line of the new file.

File Practice The files input1.txt and input2.txt each contain a list of numbers of type int in ascending order. Write a program to create a new file, output.txt, that contains all of the numbers from both input files in sorted order. Use a function that will accept as parameters any two open input streams and an open output stream.

More File Practice Write a program to read a file of data about employee raises and create a file of notes to the employees about their raises. The data is stored in the file “a:\\newSal.txt” in the form: LastName, FirstName OldSal NewSal For each line, write to “a:\\letters.txt” a note in the form: Dear FirstName LastName, You have received a raise of raisePercentage%. Your new salary is NewSal. Write 3 blank lines before each note and 3 blank lines followed by a line of dashes after each note.