Question

write a java program With a given data file called data.txt contains integer numbers, write a...

write a java program

With a given data file called data.txt contains integer numbers, write a program that will open the file and read all the integer numbers and determine how many numbers were read, the smallest number, the largest number, the sum of all numbers, and the average value. The result should be stored into a file called dataOutput.txt. The program should also sort all numbers in ascending order and output the results (10 numbers per line) into a file mentioned. Your program should implement Java ArrayList class, Java File I/O, and Java error handling excaption.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.ArrayList;
        import java.util.Collections;
        import java.util.List;
        import java.util.Scanner;

class Main{
    public static void main(String[] args) {
        //Scanner to read the data
        Scanner obj = null;
        try {
            obj = new Scanner(new File("data.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            return;
        }
        //Initiaize min and max
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int sum = 0;
        int count = 0;
        //List to store numbers
        List<Integer> list = new ArrayList();
        //Read all ints
        while(obj.hasNextInt()){
            count++;
            int x =obj.nextInt();
            //Update min and max. Find sum
            min = Math.min(x,min);
            max = Math.max(x,max);
            sum += x;
            //Add to list
            list.add(x);
        }
        //Find avg
        double avg = sum/count;
        //Print results
        System.out.println("Minimum : "+min);
        System.out.println("Maximum : "+max);
        System.out.println("Average : "+avg);
        //Sort the data
        Collections.sort(list);
        int ind = 0;
        //Print the list
        for(int x : list){
            System.out.print(x+" ");
            ind++;
            //For every 10th element, print new line
            if(ind%10==0){
                System.out.println();
            }
        }
    }
}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
write a java program With a given data file called data.txt contains integer numbers, write a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT