Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
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 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Announcements Quiz 2 Grades Posted on blackboard.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(Dreaded) Quiz 2 Next Monday.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Introduction to programming in java
Compiling and Running a Java Program
Multiple if-else boolean Data
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Testing and Exceptions
TK1114 Computer Programming
Quiz Next Monday.
Advanced Programming in Java
File Input and Output TOPICS File Input Exception Handling File Output.
CSS161: Fundamentals of Computing
Starting Out with Java: From Control Structures through Objects
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
Know for Quiz Everything through Last Week and Lab 7
Multiple if-else boolean Data
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
File Review Declare the File Stream Object Name
Differences between Java and JavaScript
Chapter 6 – Methods Topics are:
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Starting Out with Java: From Control Structures through Objects
Methods/Functions.
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized to FileOutputStream variable For input from file: –File variable initialized to filename (String) –Scanner variable initialized to File variable

import java.util.Scanner; import java.io.*; class FormatFileData { public static void main(String [ ] args) throws IOException { int loops, integer, i; float decimal; String name; File ifile = new File("mydata.txt"); Scanner scan = new Scanner(ifile); loops = scan.nextInt(); for(i= 0 ; i < loops; i++) { integer = scan.nextInt(); decimal = scan.nextFloat(); name= scan.next(); System.out.print(integer + " "); System.out.print(decimal + " "); System.out.print(name + " "); System.out.println(); } mydata.txt file Jon Bill e9 Mary Smith -3 -4e3 xyz Jon Bill E10 Mary Smith -3 – xyz Output:

Determining End of File There is a scan.hasNext() in JAVA Not used to determine if there is a String Used to Detect End of File (EOF), i.e., there is nothing left to read in the file Returns false if at EOF

Methods public static void main(String [] args) { … displayVals(); … displayVals(); … displayVals(); return(0); } public static void displayVals() { System.out.println(…); return; /*back to where we left off */ }

Methods Method: A Discrete Piece of Code that Performs a Specific Operation or Task Named with a Descriptive Identifier Called from main() or Another Method When Called, Program Control (Execution) Is Transferred to the method Method Performs Required Tasks, and then Possibly Returns a Value After Return from Method, Control Returns to the Statement Following the Method Call

Method Attributes Method Name: Identifier Used to Call method Method Parameter(s) or Argument(s): Value(s) Passed into method for Use by method Code Method Return Value: Value Returned by method Back to Calling method

Method Parameters (Arguments) May Pass as Many Parameters as Necessary to method A Copy of the Value of the Parameter Is Passed to the Method Changing the Value of the Parameter in the Method Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

Methods – Return Values Methods Are Typed According to Their Return Values: void, int, double, etc. Method Returns a Value to Calling method via return Statement

public static void main(String [ ] args) { int val = 9,resultSq; resultSq = squareVal(val); System.out.println(“squareVal returned: " + resultSq); resultSq = squareVal(12); } public static int squareVal(int numToSq) { System.out.println("In squareVal "); return(numToSq * numToSq); }