Question

The file numbers.text conisits of sequences of numbers, each sequence preceded by a header value nd...

The file numbers.text conisits of sequences of numbers, each sequence preceded by a header value nd then followed by that many integers. Read in the sequences and print their averages. When all sequences have been read in, print out the number of sequences processed.(you must use file I/O in Java)

The name of your class should be Averages.

For example, if the file numbers.text contains:

3 1 2 3
5 12 14 6 4 0
10 1 2 3 4 5 6 7 8 9 10
1 17
2 90 80

the program should produce the following output:

The average of the 3 integers 1 2 3 is 2.0
The average of the 5 integers 12 14 6 4 0 is 7.2
The average of the 10 integers 1 2 3 4 5 6 7 8 9 10 is 5.5
The average of the 1 integers 17 is 17.0
The average of the 2 integers 90 80 is 85.0
5 sets of numbers processed

Homework Answers

Answer #1

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

public class Averages {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        File file = new File("numbers.text");
        try {
            Scanner fin = new Scanner(file);
            int numSets = 0;
            while (fin.hasNextInt()) {
                int n = fin.nextInt(), num, count = 0;
                double total = 0;
                String s = "";
                for (int i = 0; i < n; i++) {
                    num = fin.nextInt();
                    total += num;
                    ++count;
                    s += num + " ";
                }
                System.out.println("The average of the " + count + " integers " + s + "is " + (total/count));
                ++numSets;
            }
            System.out.println(numSets + " sets of numbers processed");
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
        in.close();
    }
}


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write the first six lines of a hypothetical external data file named data.csv that will be...
Write the first six lines of a hypothetical external data file named data.csv that will be read without error by the following R command: mydata <- read.table ("data.csv" , skip=3 , header=FALSE , sep="," , colClasses=c ("character" , rep ("numeric" , 3 ) ) ) Lines of data.csv: 1 : 2 : 3 : 4 : 5 : 6 :
(f) Write the first six lines of a hypothetical external data file named data.csv that will...
(f) Write the first six lines of a hypothetical external data file named data.csv that will be read without error by the following R command: mydata <- read.table ("data.csv" , skip=3 , header=FALSE , sep="," , colClasses=c ("character" , rep ("numeric" , 3 ) ) ) Lines of data.csv: 1 : 2 : 3 : 4 : 5 : 6 :
Recall the Fibonacci sequence: 1,1,2,3,8,13.... In general we can generate Fibonacci-like sequences by making linear combinations...
Recall the Fibonacci sequence: 1,1,2,3,8,13.... In general we can generate Fibonacci-like sequences by making linear combinations of the formula that gives us the nth term of the sequence.Consider the following general case for generating Fibonacci-like sequences: F 1 = 1 F 2 = 1 Fn =  aFn-1 + bFn-2 where a, b are integers . Write a function that given values a, b and n will print a Fibonacci-like sequence up to the nth term of the sequence. ( eg, if...
ARM Assembly Code The Fibonacci Sequence is a series of integers. The first two numbers in...
ARM Assembly Code The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
2.3 Consider the data in the following two sequences AB Sequence Patient Period 1 Period 2...
2.3 Consider the data in the following two sequences AB Sequence Patient Period 1 Period 2 1 310 270 2 310 260 3 370 300 4 410 390 5 250 210 6 380 350 7 330 365 8 310 320 9 340 275 10 365 285 11 280 330 12 345 240 13 320 310 14 330 300 BA Sequence Patient Period 1 Period 2 1 370 385 2 310 400 3 380 410 4 290 320 5 260 340...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how I calculate the times. heres the data set and the questions. Calculate the probability that a flight will depart early or on time. Calculate the probability that a flight will arrive late. Calculate the probability that a flight departs late or arrives early. DEP_Delay ARR_Delay -4 0 -3 -3 0 -5 -7 -1 8 3 -1 -5 3 8 11 6 -6 0 -5...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT