Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be modifying the given FileReadingLab.java file in order to output the following pieces of information from given input files From the first file,

You will be modifying the given FileReadingLab.java file in order to output the following pieces of information from given input files

From the first file, you will be adding in a line count and also formatting the short string so that it can be printed in the same fashion as it is seen in the text file. Be sure to check the file itself to see how it is presented so that you can get the output correct.

From the section file, you will be giving a count of the individual numbers in the file. In addition to the count, you will also give the sum of every other number starting from the first position (or the ones in the even positions: 0, 2, 4, etc), every number starting from the second position (or the ones in the odd positions: 1, 3, 5, etc), and every third number.

Ive provided a small ints.txt file that the program initially reads from in order for you to test your second part, but you need to use the numbers.txt file in your program.

Sample Second Portion Output (from ints.txt):

count: 3 even: 2 odd: 1 thirds: 3

//Here are the specific classes we will be using. import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.util.Scanner; public class FileReadingLab{ public static void main(String[] args){ //Since this can throw several exceptions relating to input, we enclose it in a try block where we attempt to do our file operations, and // then we have a catch block for IOExceptions where we print the stackTrace (a trace of where the error occured through the classes in use) // and close the program with a non-zero (0 = fine, non-zero tells us of a problem) exit code. //This is a little more specific that catching any exception, like useing the Exception class would, but still is fairly general and could be more specific. try{ File file = new File("text.txt"); //We open our file using the File class, and pass in as an argument the location of the file. FileReader fileReader = new FileReader(file); //We then pass the file variable we just created to the FileReader class to allow us to read the specified file. BufferedReader bufferedReader = new BufferedReader(fileReader); //However, by using the BufferedReader class with the fileReader, we can be more efficient in our reads. StringBuffer stringBuffer = new StringBuffer(); //Then as we read the file we will use the StringBuffer class to create a String from what is read in from the file // no matter the type. String line; //We will use this as a holder for what we are reading in. while((line = bufferedReader.readLine()) != null){ //As long as we do not have a null String (so we are still reading lines from the file), we will continue. stringBuffer.append(line); //adds the newest line to our stringBuffer } bufferedReader.close(); //Close the bufferedReader we used since we are finished. fileReader.close(); //We close the fileReader as well. //Then we print out the contents of the file with a pre-face letting us know and then using the toString() method from the stringBuffer class. System.out.println("Contents of file:"); System.out.println(stringBuffer); //Next we look at another way to read in files, this time using the Scanner class. //We pass in a File (similar to the above, but just in one step instead of spreading it out) to our Scanner so that it will read from it as its input. Scanner fin = new Scanner(new File("ints.txt")); //Then as long as we have a next item in the file, we keep looping through it and print out the numbers as we come to them. while(fin.hasNextInt()){ System.out.print(fin.nextInt()); } }//end try block catch(IOException e){ //Here, if an IOException is thrown by any of the above statements we will catch it, and then print off from the stack the last of what was happening // so that we can have some detail of what went wrong and where. Then we exit the program. e.printStackTrace(); System.exit(1); } }//end main method }//end class FileReadingLab ~~text.txt 
I Read The Number 280 From The Input File ~~numbers.txt
5 6 8 9 5 65 15 75 85 98 65 25 45 78 54 1 5 3 5 5 7 8 57 9 4 5 4 1 2 4 8 46 1 4 5 1 8 11 8 1 8 46 1484 8 5 4 87 5 4 1 54 8 845 6 85 7 8 4 71 21 87 8 41 54 8 4 984 54 8 45 4 8 84 5 487 4 3 4 7 54 68 7 7 5 4

~~ints.txt

1 2 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

Students also viewed these Databases questions

Question

What is the terminal value of a project? How is it calculated?

Answered: 1 week ago

Question

1. How is the newspaper help to our daily life?

Answered: 1 week ago

Question

1. Prepare a short profile of Mikhail Zoshchenko ?

Answered: 1 week ago

Question

What is psychology disorder?

Answered: 1 week ago