Question

In: Computer Science

Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a...

Create a class called clsTextProcessor.java. Your program should deliver the following:

  1. Asks the user for a location of a text file, with (FileNotFound) exception handling:
    1. Keep asking for a valid file name and location
    2. If an invalid file is provided, display an error message.
    3. Once provided, proceed.
  2. Opens the text file and finds the following:
    1. Minimum value
    2. Maximum value
    3. The average value of all number in the text
  3. Prints a message displaying the values in step No. 2

Solutions

Expert Solution

ClsTextProcessor.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ClsTextProcessor {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter file name:");
       String fileName = scan.next();
       // Reading file name
       File file = new File(fileName);
       // Loop to enter valid file name and location
       while (!file.exists()) {
           System.out
                   .println("Invalid file and location. Enter valid file name:");
           fileName = scan.next();
           file = new File(fileName);
       }
       try {
           // number of values
           int count = 0;
           // max, min, sum variables initialized with 0
           int max = 0, min = 0, sum = 0;
           Scanner fileRead = new Scanner(file);
           // reading each value from file and keeping in num variable
           while (fileRead.hasNextInt()) {
               int num = fileRead.nextInt();
               if (count == 0) {
                   // file value from file assigned to min, max
                   max = min = num;
               } else {
                   if (min > num) {
                       min = num;
                   }
                   if (max < num) {
                       max = num;
                   }
               }
               // Adding each value
               sum += num;
               // count increased for each value
               count++;
           }
           // calculating average value
           double avg = sum / (double) count;
           System.out.println("Minimum value: " + min);
           System.out.println("Maximum value: " + max);
           System.out.println("The average value of all number in the text: "
                   + avg);
       } catch (FileNotFoundException e) {
           System.out.println("File is not found");
       }

   }

}

Output:

Enter file name::
D://numbers111.txt
Invalid file and location. Enter valid file name:
D://numbers.txt
Minimum value: 1
Maximum value: 10
The average value of all number in the text: 5.5

numbers.txt:

1 2 3 4 5 6 7 8 9 10


Related Solutions

Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
[4 marks] Write a Python program for a “Deliver-Eat!” app that asks the user for their...
[4 marks] Write a Python program for a “Deliver-Eat!” app that asks the user for their tip amount. If the user tips less than $5 they should receive a 1-star rating. If the user tips $5 or more they should receive a 5-star rating. Your program should match the output below. User input is in red. >>> How much would you like to tip? $4.50 >>> Here is your rating! * >>> How much would you like to tip? $7.20...
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Create a program that asks the user for the names of two car dealerships and the...
Create a program that asks the user for the names of two car dealerships and the # of cars sold in each one. Then output that data in two columns as shown below. The "Store location" column has a width of 25, while the "Cars sold" column has a width of 9. Also, notice the alignment of the second column. The program should end with the "Press Enter to end this program" prompt. OUTPUT Enter the location for the first...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
In a file called LengthSum.java, write a program that: Asks the user to enter a string....
In a file called LengthSum.java, write a program that: Asks the user to enter a string. Asks the user to enter a second string. Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please enter a second...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT