QuestionQuestion

First, create a file called "input.txt" that contains a sequence of integer values separated by one or more spaces or returns. You can use the Notepad to create the file. When you save the program choose the "text file" option and name your file "input" because Notepad will automatically append the extension ".txt". Put this file in the folder you are going to use for the following Java programs (anyway, know where your text file is located, so that you can give a full path in case it isn't in the same folder as your Java program). You will use this file in the programs below.

1. Write a Java program that reads the file "input.txt" and calculates and prints the average of all integer numbers in this file.

SAMPLE OUTPUT:
throws FileNotFoundException try/catch
OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5
6 x x x 8 12 x x 34 x x 10 20
Number # 1 is: 1
Number # 2 is: 2
Number # 3 is: 3
Number # 4 is: 4
Number # 5 is: 5
Number # 6 is: 6
Number # 7 is: 8
Number # 8 is: 12
Number # 9 is: 34
Number # 10 is: 10
Number # 11 is: 20
The average of 11 numbers/file = 9.55
OUTPUT SAMPLE #2 for input.txt: xxxxx x x
x x xx x x x x x
The file doesn't contain any integers.
Exit program!
OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5
6 x x x 8 12 x x 34 x x 10 20
Please input the name of the file to be
opened: input.tx
--- File Not Found! ---
OUTPUT SAMPLE #2 for input.txt: 1 2 3 4 5
6 x x x 8 12 x x 34 x x 10 20
Please input the name of the file to be
opened: input.txt
Number # 1 is: 1
Number # 2 is: 2
Number # 3 is: 3
Number # 4 is: 4
Number # 5 is: 5
Number # 6 is: 6
Number # 7 is: 8
Number # 8 is: 12
Number # 9 is: 34
Number # 10 is: 10
Number # 11 is: 20
The average of 11 numbers/file = 9.55
OUTPUT SAMPLE #3 for input.txt: xxxxx x x
x x xx x x x x x

2. Write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt."
SAMPLE OUTPUT:
throws FileNotFoundException try/catch
OUTPUT SAMPLE #1 for input.txt: xx x xx
10 12 1 3 5 34 x 2 5 7 9 x x 44
Found 11 numbers/file.
Open the file output.txt to get the
output.
output.txt: 10 12 34 2 44
OUTPUT SAMPLE #2 for input.txt: xx xx xx
x x x xxx xxx
The file doesn't contain any integers.
Exit program!
OUTPUT SAMPLE #1:
Please input the name of the file to be
opened: input.tx
--- File Not Found! ---
OUTPUT SAMPLE #2 for input.txt: xx x xx
10 12 1 3 5 34 x 2 5 7 9 x x 44
Please input the name of the file to be
opened: input.txt
Found 11 numbers/file.
Open the file output.txt to get the
output.
output.txt: 10 12 34 2 44
OUTPUT SAMPLE #3 for input.txt: xx xx xx
x x xxx x xx x x x
Please input the name of the file to be
opened: input.txt
The file doesn't contain any integers.
Exit program!

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

public class ReadFile {

public static void main(String[] args)
{
Scanner inFile;
Scanner input = new Scanner(System.in);
try {
inFile = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
System.out.print("Please input the name of the file to be opened: ");
try {
inFile = new Scanner(new File(input.next()));
} catch (FileNotFoundException e1) {
System.out.println("--- File Not Found! ---");
return;
}
}
int total = 0, count = 0;
double average;

while(inFile.hasNextInt())
{
count++;
int num = inFile.nextInt();
System.out.println("Number # " + count + " is: " + num);
total+= num;
}

average = (double)total/count;
System.out.println("The average of " + count + " numbers/file = " + average);

}
}...
$14.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats