import java io import java util public class
import java. io. *; import java. util. *; public class echo. File { public static void main(String[] args) throws File. Not. Found. Exception { Scanner console. In = new Scanner(System. in); System. out. print("Input file: "); String in. Filename = console. In. next. Line(); File in. File = new File(in. Filename); Scanner input. File = new Scanner(in. File); System. out. print("Output file: "); String out. Filename = console. In. next. Line(); File out. File = new File(out. Filename); Print. Stream output. File = new Print. Stream(out. File); String my. String; while ( input. File. has. Next. Line() ) { my. String = input. File. next. Line(); output. File. println(">" + my. String); } System. out. println("Done"); }} Copyright 2006 by Pearson Education 1
Input file: Sue. txt Output file: out. Sue. txt Done Sue. txt Sue, la de dah Thanks, Bob out. Sue. txt >Sue, >la de dah >de dah >Thanks, >Bob Copyright 2006 by Pearson Education 2
import java. io. *; Combining some import java. util. *; statements public class echo. File 2 { public static void main(String[] args) throws File. Not. Found. Exception { Scanner console. In = new Scanner(System. in); System. out. print("Input file: "); String in. Filename = console. In. next. Line(); Scanner input. File = new Scanner(new File(in. Filename)); System. out. print("Output file: "); String out. Filename = console. In. next. Line(); Print. Stream output. File = new Print. Stream(new File(out. Filename)); while ( input. File. has. Next. Line() ) output. File. println(">" + input. File. next. Line()); System. out. println("Finished copying file " + in. Filename + " to “ + out. Filename); }} Copyright 2006 by Pearson Education 3
import java. io. *; Combining even more statements. import java. util. *; Notice: No String public class Echo. File 3 { variables. public static void main(String[] args) throws File. Not. Found. Exception { Scanner console. In = new Scanner(System. in); System. out. print("Input file: "); Scanner input. File = new Scanner(new File(console. In. next. Line())); System. out. print("Output file: "); Print. Stream output. File = new Print. Stream(new File(console. In. next. Line())); while ( input. File. has. Next. Line() ) output. File. println(">" + input. File. next. Line()); System. out. println("Done"); } Copyright 2006 by Pearson Education 4
Building Java Programs Chapter 7: Arrays Copyright 2006 by Pearson Education 5
Chapter outline n array basics n n n array traversal algorithms n n declaring and initializing an array getting and setting values of elements of an array limitations of arrays for counting and tallying printing an array's elements searching an array reversing an array advanced array usage n n n arrays as parameters to methods String and Graphics methods that use arrays the Arrays class command-line arguments shifting elements in an array Copyright 2006 by Pearson Education 6
Array basics reading: 7. 1 Copyright 2006 by Pearson Education 7
A problem we can't solve (yet) n Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44. 6 4 days were above average. n We need the temperatures to compute the average, and again to tell how many were above average. Copyright 2006 by Pearson Education 8
Why the problem is tough n We appear to need each input value twice: n n n We could read each value into a variable. . . n n once to compute the average a second time to count how many were above average However, we don't know how many variables to declare. We don't know how many days' weather will be typed until the program is running. We need a way to declare many variables in one step. Copyright 2006 by Pearson Education 9
Activity: Find the difference between each element of the following array and the average value in the array Let’s do it ourselves. List of numbers 2 6 8 2 9 1 8 2 6 6 Average ____ Copyright 2006 by Pearson Education Difference from average 10
Activity: Find the difference between each element of the following array and the average value in the array Let’s do it ourselves. List of numbers 2 6 8 2 9 1 8 2 6 6 Average __5__ Copyright 2006 by Pearson Education Difference from average -3 1 3 -3 4 -4 3 -3 1 1 11
Arrays n array: A variable that stores many values of the same type. n n n element: One value in an array. index: A 0 -based integer used to access an element from an array. We usually draw an array as a row or column of boxes. n Example: an array of ten integers index 0 1 value 12 49 element 0 Copyright 2006 by Pearson Education 2 3 4 5 6 -2 26 5 17 -6 element 4 7 8 84 72 9 3 element 9 12
Array declaration n Declaring/initializing an array: <type> [] <name> = new <type> [ <length> ]; n n n The length of the array is specified between [ ] brackets. Example: int[] numbers = new int[10]; index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0 The array's length can be any expression. n Example: int len = console. next. Int; int[] data = new int[len]; Copyright 2006 by Pearson Education 13
Array auto-initialization n When arrays are initially constructed, every element is automatically initialized to a "zero-equivalent" value. n n n int: double: boolean: char: String or object: 0 0. 0 false '