6 Exercises – Reading from Text File

Exercise 1

Screen Shot 2017-05-11 at 10.29.43 PM.png

Screen Shot 2017-05-11 at 10.23.51 PM.png

The first step is to read the file. Then I store the text from the file in a String called input. Then I loop the characters in input and output them line by line.

Exercise 2

Screen Shot 2017-05-11 at 10.29.46 PM.png

Screen Shot 2017-05-11 at 10.33.16 PM.png

The first step is to read the file. Then a declare an integer variable called line and initiate it with the value of zero. Then I loop the text in the file. As long as the text still has next line, the value of line will keep adding one. Until there is no line left, the system will output the value of the variable line.

Exercise 3

Screen Shot 2017-05-11 at 10.29.50 PM.png

Screen Shot 2017-05-11 at 10.36.28 PM.png

The first step is to read the file. Then I loop the text to figure out the number of lines in this file.  Then I create an array called gradeArray to store the grade(int type) on each line by looping the whole text. Then the sum can be calculated by adding all the grades in gradeArray. The average is the sum divided by the numbers of grades which equal to the numbers of lines.

Exercise 4

Screen Shot 2017-05-11 at 10.29.58 PM.png

Screen Shot 2017-05-11 at 10.40.53 PM.png

I create a double loop here. The process will be comparing the first line in inputStream1 to all the lines in inputStream2, comparing the second line in inputStream2 to all the lines in inputStream2, and continue looping the following lines in inputStream1. If all characters in a line from inputStream1 are exactly the same as those of a line from inputStream2, the integer called same will add 1, otherwise, diff will add one.

Exercise5

Screen Shot 2017-05-09 at 8.55.50 PM.png

Screen Shot 2017-05-11 at 10.45.11 PM.png

As long as a line contains “=”, the “=” notation will be replaced by the “:” notation. As long as a line contains “&”, the “&” notation will be replaced by the a space.

Exercise 6

Screen Shot 2017-05-09 at 8.55.55 PM

Screen Shot 2017-05-11 at 10.47.33 PM.png

Screen Shot 2017-05-28 at 下午2.17.12

Screen Shot 2017-05-28 at 下午2.17.19

Screen Shot 2017-05-28 at 下午2.17.28Screen Shot 2017-05-28 at 下午2.17.33Screen Shot 2017-05-28 at 下午2.17.46Screen Shot 2017-05-28 at 下午2.17.56

Conclusion

We should really benefit from the OOP function, internal call, external call that Java has. Just like exercise 6, it makes the program more efficient and well-organized.

留下评论