Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to calculate min, max and average and write the output into into a text file in java

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

public class ExternalData {

public static void main(String[] args) {

    try {
        System.out.print("Enter the file name with extension : ");

        Scanner input = new Scanner(System.in);

        File file = new File(input.nextLine());

        input = new Scanner(file);

        while (input.hasNextLine()) {
            int min = input.nextInt();
            while(input.hasNextInt());
            int num = input.nextInt();
            if(num < min) {
                min = num;
            }

            while (input.hasNextLine()) {
                int max = input.nextInt();
                while(input.hasNextInt());
                int num2 = input.nextInt();
                if(num2 > min) {
                    min = num;

            String avg = input.nextLine();




            Formatter f = new Formatter("C:UsersKevinDropboxKevin 
            Carter-8042Intro to Soft EngTask 12outputTest.txt");

            // print the formatted strings to the file
            f.format("The min of [1,2,3,4,5,6] is " + min);

            f.format("

The max of [1,2,3,4,5,6] is " + max);

            f.format("

The avg of [1,2,3,4,5,6] is " + avg);
            f.close();    
            }     

            } catch (Exception ex) {
              ex.printStackTrace();
            }

          }

       }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #calculate #max #average #write #output #text #file #java
ADD COMMENT
Topic
Name
5+8 =