how to calculate min, max and average and write the output into into a text file in java
Java
package TrialPrograms;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) throws IOException {
FileInputStream fi = new FileInputStream("F:\\Test\\file.txt");
FileOutputStream fo = new FileOutputStream("F:\\Test\\output.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fi));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] arr = strLine.split(" ");
String[] nos = arr[1].split(",");
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i<nos.length; i++){
int no = Integer.parseInt(nos[i]);
set.add(no);
}
TreeSet<Integer> sortedSet = new TreeSet<Integer>(set);
switch(arr[0]) {
case "Min:":
String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
bw.write(msg1);
bw.newLine();
break;
case "Max:":
String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
bw.write(msg2);
bw.newLine();
break;
case "Avg:":
Object[] noarray = sortedSet.toArray();
int noarraysize = noarray.length-1;
int sum=0;
for(int i=0;i<=noarraysize;i++) {
int no=Integer.valueOf(noarray[i].toString());
sum = sum + no;
if(i==noarraysize) {
String msg3="The Avg of [" +arr[1]+ "] is " +(double)sum/noarray.length;
bw.write(msg3);
bw.newLine();
}
}
break;
case "Sum:":
Object[] noarray1 = sortedSet.toArray();
int noarraysize1 = noarray1.length-1;
int sum1=0;
for(int i=0;i<=noarraysize1;i++) {
int no=Integer.valueOf(noarray1[i].toString());
sum1 = sum1 + no;
if(i==noarraysize1) {
String msg4="The Sum of [" +arr[1]+ "] is " +sum1;
bw.write(msg4);
bw.newLine();
}
}
break;
}
}
br.close();
bw.close();
}
}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:\\Users\\Kevin\\Dropbox\\Kevin
Carter-8042\\Intro to Soft Eng\\Task 12\\outputTest.txt");
// print the formatted strings to the file
f.format("The min of [1,2,3,4,5,6] is " + min);
f.format("\n\nThe max of [1,2,3,4,5,6] is " + max);
f.format("\n\nThe avg of [1,2,3,4,5,6] is " + avg);
f.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Also in Java:
- Title
- check if map contains key java
- Category
- Java
- Title
- java php object
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- java get command line input
- Category
- Java
- Title
- keep jframe on top
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- in javascipt how to stop further page processing
- Category
- Java
- Title
- java hashmap put
- Category
- Java
- Title
- how to instanciate map.entry java
- Category
- Java
- Title
- how to add java_home in mac
- Category
- Java
- Title
- spigot run task later
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- to char array java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- countdown timer with seekbar
- Category
- Java
- Title
- java file download
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- int to binary java
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- declare bufferedreader java
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- processing string to int
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- processing sound library
- Category
- Java
- Title
- java command line arguments
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- Java create array of array
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- arraylist in java
- Category
- Java
- Title
- on click android studio not working
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- initialize applet in java
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- Java copy file
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- java read string input
- Category
- Java
- Title
- split every character in string into array java
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- java arraylist
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- javafx action event enter key
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- arraylist java methds
- Category
- Java
- Title
- is self divisor java
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- enum with numbers java
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- what is static setter and getter examples in java
- Category
- Java
- Title
- raspberry stackexchange how to install the java jdk
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- java valeur absolue
- Category
- Java
- Title
- partial view tiles spring
- Category
- Java
- Title
- loop collection android
- Category
- Java
- Title
- Manifest merger failed androidx
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- casting java
- Category
- Java