DOC PREVIEW
TRINITY CSCI 1320 - Lecture Notes

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Reading from Files10-13-2010Opening DiscussionMidterm results.Minute essay commentsWhat are floats?MotivationPrograms are more useful when they can interact with files.Everything that isn't in a file is lost when the program stops running.I/O RedirectionUsing I/O redirection gives you some very basic ability to read from and write to files.It has big limitations though because there is only one file each way.More over, that one file blocks the ability to use either standard input or output.Packages and ImportsTo read from a file we will be using the scala.io.Source type. To understand what that means, we need to talk about packages.Packages provide a way to organize code and group things of like functionality.Import statements let you use things without typing in their fully specified names.The APITo get a sense of the different package in Scala, it is helpful to look at the API.There are still lots of things in the API you won't fully understand. That isn't a problem as you aren't expected to get too much from it right now.scala.io.SourceCall Source.fromFile(fileName:String) to get a Source object that reads from a file.There are other methods in the main Source object that we will learn about later.The fromFile method technically gives you BufferedSource. This is for efficiency.IteratorsBoth Source and BufferedSource are of the type Iterator[Char].An Iterator has most of the methods you are used to from List and Array. However, you can only go through it once.Fundamentally uses hasNext and next methods.getLinesThis will give you an Iterator[String] that will go through the file one line at a time instead of a character at a time.You will often find this more useful.The split MethodEven full lines isn't always what you want. You might want multiple values for each line.As you saw on the midterm, the split method of String will break up the String on a delimiter and give you back an array.Delimiter is regular expression. Use + for 1 or more and * for 0 or more.Let's try using this to get something useful.Minute EssayWhat questions do you have?Interclass problem:Write a script that will read a file that x and y values for points on each line and print the shortest distance between any two


View Full Document

TRINITY CSCI 1320 - Lecture Notes

Documents in this Course
Functions

Functions

10 pages

Functions

Functions

10 pages

Graphics

Graphics

10 pages

Graphics

Graphics

11 pages

Loops

Loops

4 pages

Loops

Loops

3 pages

Strings

Strings

9 pages

Functions

Functions

10 pages

Loops

Loops

11 pages

Graphics

Graphics

11 pages

Graphics

Graphics

12 pages

Sorting

Sorting

11 pages

Sorting

Sorting

10 pages

Arrays

Arrays

10 pages

Loops

Loops

18 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?