Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a program that asks the user to enter the name of a file that contains

ID: 3600485 • Letter: 2

Question

2. Write a program that asks the user to enter the name of a file that contains a set of integer values, and then asks the user to enter an integer number. The program should display the average of the numbers that are greater than the input number. User Notepad or another text editor to create a simple file that can be used to test the program. First, the user enters the file name as below: Enter the file name: myText.txt and if, for example, the file 'mytext.txt' contains the following numbers 30 23 56 43 32 35 90 12 and then, if the user enters an integer number, as The number to check: 40 the program should display the output as The average of the numbers that are greater than 40 is 58.25 Note that average of numbers 44, 56, 43 and 90 is 2334-5825. The average must be shown up to two decimal points. (Hint: DecimalFormat class can be used) If the input file is not found, the user must be prompted to enter the filename again with the following message "File is not found, enter the filename again".

Explanation / Answer

myText.txt

30
44
23
56
43
2
32
35
90
12

______________

FindAverage.java

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

public class FindAverage {

public static void main(String[] args) {
// Declaring variables
int num = 0, check, count = 0;
double tot = 0, avg = 0.0;
String filename;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the filename entered by the user
System.out.print("Enter the file name :");
filename = sc.next();

/* This while loop continues to execute
* until the user enters a valid file number
*/
while (true) {
try {
Scanner sc1 = new Scanner(new File(filename));

// Getting the check entered by the user
System.out.print("The number to check: ");
check = sc.nextInt();

// Reading the numbers from the file
while (sc1.hasNext()) {
num = sc1.nextInt();
// calculate total of number which are greater than check number
if (num > check) {
tot += num;
count++;
}
}
sc1.close();
break;
} catch (FileNotFoundException e) {
System.out.println("File is not found, enter the filename again.");
System.out.print("Enter the file name :");
filename = sc.next();
continue;
}
}


// calculate average
avg = tot / count;


// Displaying the average
System.out.println("The average of the numbers that are greater than " + check + " is " + avg);
}

}

________________

Output:

Enter the file name :hello.txt
File is not found, enter the filename again.
Enter the file name :myText.txt
The number to check: 40
The average of the numbers that are greater than 40 is 58.25

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote