8 File Input

8 File Input - File Input File Input In “real life”...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: File Input File Input In “real life” many programs use very little graphics or none at all Many programs process large amounts of data that is stored as text For example, the school district has large files of names, addresses, course information and grades. It runs computer programs that use those files to print grade reports. 1 Creating a program that uses File Creating a program that uses File Input in processing Start a new program For now, we don’t need any functions The loadStrings() example from the API is a good starting point: String lines = loadStrings("list.txt"); println("there are " + lines.length + " lines"); for (int i=0; i < lines.length; i++) for { println(lines[i]); println(lines[i]); } Save the program 2 Save it someplace where you can find it (like the desktop) and name it list (or whatever you like) 3 Then, create a text file in notepad Then, create a text file in 4 Save it someplace where you can Save it someplace where you can find it (like the desktop) and name it list (or whatever you like) 5 Now start processing, and add the Now start processing, and add the file to your program using Sketch | Add File. . . 6 Browse to your file and open it Browse to your file and open it 7 Make sure the name is right Make sure the If the name in our code doesn’t match the name of the file, our program won’t run. Otherwise, we can then load it into an Array of Strings with code like: String lines = loadStrings("list.txt"); 8 list.length list.length The length of the array will be the number of lines there are in the text file println( "there are " + list.length + " lines"); list.length 9 list.length list.length In this case there are 6 println( "there are " + list.length + " lines"); list.length 10 loadStrings() from the API loadStrings() This loop will display each line of input on a line by itself for (int i=0; i < list.length; i++) for { println(list[i]); println(list[i]); } 11 Now that we know the program is Now that we know the program is working, we can begin to modify it Our assignment is to write a program that converts Fahrenheit temperatures to Celsius temperatures We’ll have a text file with three integers: 212 32 -40 12 Converting a String to an int Converting a to an loadStrings() always returns an array of Strings This is a little awkward if you want to work with integers or decimals int fahrenheit = lines[i]; //error! 13 the int() function the The problem is that the types don’t match String lines = loadStrings("list.txt"); lines //lots of java not shown int fahrenheit = lines[i]; 14 the int() function the the int() function fixes the problem String lines = loadStrings("list.txt"); lines //lots of java not shown int fahrenheit = int(lines[i]); It converts a String to an int, for example "6" with 6 15 Practice quiz question: If we use the Practice quiz question: If we use the following data file, what will be the output? String lines = loadStrings("list.txt"); println("there are " + lines.length + " lines"); ("there lines.length for (int i=1; i < lines.length; lines.length; { println(lines[i]); println } i=i+3) i=i+3 16 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online