Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a new Java file, declare a new class and the main method. The class name should be CountNumbersFromFile. 2. On top of the

1. Create a new Java file, declare a new class and the main method. The class name should be CountNumbersFromFile.

2. On top of the file import java.io.* and java.util.Scanner.

3. Write another method and name it countNumbers. This method should take in a filename as a parameter and the return type should be void. Since this method will be working with a file, it needs to include throws IOException before the opening curly bracket for the body of the method.

4. Inside of the method countNumbers create a File object. Into the constructor of the file object pass the filename, parameter which is being passed into the method. Refer to the lecture slides for hints.

5. Next, write an if statement to check if the file exists. If the file does not exist, then exit the program with a System.exit(0) statement. Refer to the lecture slides for help.

6. Then create a Scanner object and pass into the constructer the file reference. Take a look at the lecture slides, it has this code on there if you need help.

7. You need to declare 4 variables which will keep track of information about the numbers in the file. You need to keep track of the following values when you go through the file:Number of linesSum of all of the numbersFrequency of the number 1Frequency of the number 5

8. Write a while loop, the condition for the while loop should be using the method hasNext from the Scanner class. This method will return true, as long as there is another line in the text file.

9. Inside of the while loop use the method nextInt to read the number from the line. This method belongs to the Scanner class. Increment the variables which you have declared outside of the loop as needed.

10. After the while loop, make sure to close the Scanner. This will close up the file and release any resources which may be open for reading the file.

11. Make 4 print statements and print out the results to the console. Take a look at the sample output in Figure 1. If you wrote the code correctly your output should be the same.

12. Dont forget to call the countNumbers method from the main method. Compile and run your code. Fix any syntax errors which may occur.

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

Step: 3

blur-text-image

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

List out some inventory management techniques.

Answered: 1 week ago