Reading External Files By: Greg Patterson APCS 2010.

Slides:



Advertisements
Similar presentations
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);
Advertisements

Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
CS102--Object Oriented Programming
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Chapter 2 Review Questions
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
FIT FIT1002 Computer Programming Unit 19 File I/O and Exceptions.
Files and Streams CS 21a Chapter 11 of Horstmann.
Program Design and Development
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
THE BIG PICTURE. How does JavaScript interact with the browser?
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Lecture 2 Objectives Learn about objects and reference variables.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Winter 2006CISC121 - Prof. McLeod1 Last Time Wrapper classes JFileChooser (along with JOptionPane, and JColorChooser !) Text File Output.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
Creating a GUI Class An example of class design using inheritance and interfaces.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Foundations of Programming: Java
Loops A loop is: Java provides three forms for explicit loops:
CSC111 Quick Revision.
Introduction to programming in java
Java Programming Lecture 2
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.
Streams and File I/O.
Introduction to Methods in java
Unit 6 Working with files. Unit 6 Working with files.
File Handling in Java January 19
Python programming exercise
Tutorial 10: Programming with javascript
Unit 3: Variables in Java
Welcome back! October 11, 2018.
Input, Variables, and Mathematical Expressions
Intro to Programming (in JavaScript)
Presentation transcript:

Reading External Files By: Greg Patterson APCS 2010

Why This is Useful Java has the ability to read external data files containing numbers, strings, or really anything you want. It can then manipulate this data in a various number of ways that save time for the user. For instance, you could count the number of times “Hello” appears in a document containing 4,000 strings.

The Syntax In order to read external data files, the first thing that must be done, before even opening your class, is to import the following parts: import java.util.Scanner; import java.io.*; These imports enable you to import files and utilize the functions of the Scanner class.

Syntax cont. After opening your class and main method, the next step is to add the following line: Scanner inFile = null; Obviously the Scanner can be named whatever the code author wishes, but the generally accepted name in APCS is inFile. This line of code creates a Scanner that will be used later to read the external file into the program.

Syntax cont. The next (and last) block of code needed to read an external file is a try-catch statement in which the file is tried and caught only if the file named in the try statement is not found. The syntax will be on the next slide.

Syntax cont. try { inFile = new Scanner(new File(“fileName.ext”)); } catch (FileNotFoundException e) { System.out.print(“File not found!”); System.exit(0); } Instead of fileName.ext, you would put the name of your file. For instance Prog365h.dat. With all of this in your program and the file name spelled correctly, the program will scan and read your external file. Now let’s discuss what you can do with this.

Useful Methods inFile.hasNext() - This method would go in a while or do-while loop and would run as long as there is more data to be read from the file. inFile.next() - This method, reads the next bit of data from the file into the program and stores it in a String. It and its variations go hand-in-hand with the hasNext method, as when all of the data has been read, the loop will stop.

Useful Methods cont. inFile.nextInt() - This method reads specifically the next Integer in the file and puts it in an int variable. inFile.nextDouble() - This method reads specifically the next Double in the file and puts it in a double variable.

That’s all the basics about external file reading. It can take a bit of time to get used to the syntax and the functions of the different methods but after writing a few programs it becomes second nature. I hope you learned a lot from this power point and take that knowledge with you as you write your programs.