Can someone please add the following to the code listed below? I am having issue
ID: 3572163 • Letter: C
Question
Can someone please add the following to the code listed below? I am having issues adding it to the existing code.
catch (FileNotFoundException exception) {
System.out.println("File not found."); }
catch (NoSuchElementException exception) {
System.out.println("File contents invalid."); }
catch (IOException exception) {
exception.printStackTrace();
Code:
package calcweightedavgwithexceptions2;
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;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
public class CalcWeightedAvgWithExceptions2 {
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("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();
}
}
}
Explanation / Answer
Below is the code as per your requirement.
package calcweightedavgwithexceptions2;
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;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
public class CalcWeightedAvgWithExceptions2 {
public static void main(String arg[]) throws IOException{
try
{
ArrayList inputValues = getData();
System.out.println("Input Valules :"+inputValues);
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
}
catch (FileNotFoundException exception)
{
System.out.println("File not found.");
}
catch (NoSuchElementException exception)
{
System.out.println("File contents invalid.");
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
public static ArrayList getData() throws FileNotFoundException {
File file=new File("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{
throw new FileNotFoundException();
}
return data;
}
public static double calcWeightedAvg(ArrayList list) throws NoSuchElementException
{
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");
}
}
feel free to ask if you have any doubt :)