In: Computer Science
Java Class
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ReadInData {
   public static double readData(String fileName)
{
       File file = new
File(fileName);
       Scanner scan;
       double sum = 0;
       try {
           scan = new
Scanner(file);
           int numOfValues
= Integer.parseInt(scan.nextLine()); // Read in Val for #
values
           for(int i = 0; i
< numOfValues; i++) { // for-loop to get that many values
          
    sum += scan.nextDouble(); // add the values
together (doubles)
           }
          
scan.close();
           return sum; //
return the sum of all the values in the file
       }//add catch statements
here      
   }
   public static void main(String[] args) {
      
System.out.println(readData("Test3.txt"));
      
System.out.println(readData("Test2.txt"));
      
System.out.println(readData("Text4.txt")); // notice spelling of
the file name!
      
System.out.println(readData("Test4.txt"));
   }
}
Test2.txt
6 5 3.0 "9.8" -19.8 -54.1 85.5
Test3.txt
4.0 5.0 6.2 4.2 1.0
Test4.txt
5 4.2 3.1 2.2 6.1
HELLO I AM ADDING CODE HERE
WITH OUTPUT
PLEASE GO THROUGH IT
THANK YOU
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ReadInData{
   public static double readData(String fileName) {
       File file = new File(fileName);
       Scanner scan;
       double sum = 0;
       try {
           scan = new Scanner(file);
           int numOfValues = Integer.parseInt(scan.nextLine()); // Read in Val for # values
           for(int i = 0; i < numOfValues; i++) { // for-loop to get that many values
               sum += scan.nextDouble(); // add the values together (doubles)
           }
           scan.close();
     }//add catch statements here 
     catch(FileNotFoundException ex)
     {
       System.out.println("Provided file not found");
     }
     catch(NumberFormatException ex)
     {
       System.out.println("Format of the number is not correct");
     }
     catch(InputMismatchException ex)
     {
       System.out.println("Hey Input is not matching correctly");
     }
     catch(RuntimeException ex)
     {
       System.out.println("Runtime exception occured ");
     }
     catch(Exception ex)
     {
       System.out.println("Hey something went wrong! Not able to find it ");
     }
     return sum;
   }
   public static void main(String[] args) {
       System.out.println(readData("Test3.txt"));
       System.out.println(readData("Test2.txt"));
       System.out.println(readData("Text4.txt")); // notice spelling of the file name!
       System.out.println(readData("Test4.txt"));
   }
}
OUTPUT
