Can someone please remove the exceptions and buffers from th
Can someone please remove the exceptions and buffers from this code? I am trying to do it but I am running into errors that I cannot get past. The majority of the code needs to stay the same.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CalcWeightedAvgDropLowest{
public static void main(String arg[]) throws IOException{
ArrayList inputValues = getData();
System.out.println(\"Input Valules :\"+inputValues);
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
}
public static ArrayList getData() {
File file=new File(\"D:\\\\data.txt\");
ArrayList<Double> data = new ArrayList<Double>();
if(file.exists()){
try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
String sCurrentLine;
String values[] = null;
while ((sCurrentLine = br.readLine()) != null) {
values = sCurrentLine.split(\" \");
}
for(int i=0; i<values.length; i++)
data.add(Double.parseDouble(values[i]));
}
catch(IOException e){
e.printStackTrace();
}
}
else{
System.out.println(\"File does not exist\");
}
return data;
}
public static double calcWeightedAvg(ArrayList list){
double enteredWeight = Double.parseDouble(list.get(0).toString());
int numberofLowetValuesDropped =(int) Double.parseDouble(list.get(1).toString());
list.remove(1);
list.remove(0);
Collections.sort(list);
double total = 0;
for(int i=numberofLowetValuesDropped; i<list.size(); i++){
total = total + Double.parseDouble(list.get(i).toString());
}
list.add(0, enteredWeight);
list.add(1, numberofLowetValuesDropped);
double avg = total/(list.size() - numberofLowetValuesDropped);
return avg;
}
public static void printResults(ArrayList list, double weight) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the output file:\");
String outputfile = scan.next();
try (FileWriter fw = new FileWriter(new File(outputfile)))
{
String s = \"The weighted average of the numbers is \"+weight+\", \" +
\"when using the data \"+list+\", where \"+list.get(0)+\" is the weight used, \" +
\"and the average is computed after dropping the lowest \"+list.get(1)+\" values.\";
System.out.println(s);
fw.write(s);
fw.flush();
fw.close();
System.out.println(\"Data has been written into output file\");
}
catch(IOException e){
e.printStackTrace();
}
}
}
Solution
Hi, I have removed TRY CATCH block from your code.
I have not removed Buffred Reader/Writer, because you have not mentioned that with what code you want to replace Buffered/FIleWriter.
Please let me know in case of any issue.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CalcWeightedAvgDropLowest{
public static void main(String arg[]) throws IOException{
ArrayList inputValues = getData();
System.out.println(\"Input Valules :\"+inputValues);
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
}
public static ArrayList getData() throws IOException {
File file=new File(\"D:\\\\data.txt\");
ArrayList<Double> data = new ArrayList<Double>();
if(file.exists()){
BufferedReader br = new BufferedReader(new FileReader(file));
String sCurrentLine;
String values[] = null;
while ((sCurrentLine = br.readLine()) != null) {
values = sCurrentLine.split(\" \");
}
for(int i=0; i<values.length; i++)
data.add(Double.parseDouble(values[i]));
}
else{
System.out.println(\"File does not exist\");
}
return data;
}
public static double calcWeightedAvg(ArrayList list){
double enteredWeight = Double.parseDouble(list.get(0).toString());
int numberofLowetValuesDropped =(int) Double.parseDouble(list.get(1).toString());
list.remove(1);
list.remove(0);
Collections.sort(list);
double total = 0;
for(int i=numberofLowetValuesDropped; i<list.size(); i++){
total = total + Double.parseDouble(list.get(i).toString());
}
list.add(0, enteredWeight);
list.add(1, numberofLowetValuesDropped);
double avg = total/(list.size() - numberofLowetValuesDropped);
return avg;
}
public static void printResults(ArrayList list, double weight) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the output file:\");
String outputfile = scan.next();
FileWriter fw = new FileWriter(new File(outputfile));
String s = \"The weighted average of the numbers is \"+weight+\", \" +
\"when using the data \"+list+\", where \"+list.get(0)+\" is the weight used, \" +
\"and the average is computed after dropping the lowest \"+list.get(1)+\" values.\";
System.out.println(s);
fw.write(s);
fw.flush();
fw.close();
System.out.println(\"Data has been written into output file\");
}
}



