Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* I need to create a program that prompts the user for a filename (.txt), with

ID: 643464 • Letter: #

Question

/* I need to create a program that prompts the user for a filename (.txt), with the FileNotFoundException thrown. In particular my program will take in a txt document of students grades. there are 74 students and 11 different weighted marks. I want to be able to print out the line of given marks as an array, then perform calculations on the elements in that array as to get the weighted percent of each assignemnt and print out that array. Then I need to calculate the sum of those weighted marksto obtain a final grade. I am having particular trouble with double converting to double[] and etc. My program then requires me to create a new text firl with a final grade format with max and min grade, average, and how many got an A+, B etc.*/

//This is the code I have written so far, it isnt compiling anymore and I am lost at sea! //If you can at least help me as to print out each line with there array and weighted //grade array and final grade sum

/* NOTE the text doc is a document consisting of 74 rows and 11 columns of grade components like so:

max grade for each component:

5 10 10 10 10 10 10 10 20 35 110

weight for each component:

5 4 4 4 4 4 4 4 15 15 37

*/

import java.util.*;

import java.io.*;

public class Lab7 {

   public static void main(String[] args) {

      

       Scanner userInput = new Scanner(System.in);

       Scanner fileReader = null;

       //ask user to enter the file name

       //scan the entry and assign the variable

      

       //create File of that file name

       //create a Scanner to link to that File.

   int numLines = 0;

   while (fileReader==null) {  

   try {

       System.out.print("Enter a file name: ");

       String fName = userInput.next();

      

       File inputFile = new File(fName);

       fileReader = new Scanner(inputFile);

      

       numLines = getNumEntries(inputFile);

      

      

       } catch (FileNotFoundException e) {

           System.out.println("Can't scan file!");

       }

       System.out.println("Success, total lines: " +numLines);

  

       double[] totalValues = calculateTotals(fileReader, numLines);

       printMethod(totalValues);

       //int smallestValue = min(totalValues);

       //System.out.println("Smallest value found is: " +smallestValue);

   }

      

  

   }

   // print method for printing what is in the array

   public static void printMethod(double [] arr) {

  

   //prints out contenets of array

  

       for (int i=0; i<arr.length;i++) {

  

           //int [] num = arr[i];

           System.out.print(arr[i] + " ");

       }

       System.out.println();

   }

   public static int getNumEntries(File fIn){

       //create variable to keep count of number of lines

      

      

      

       //try and create a scanner that lines to the file (again)

           //next loop through each line in the file (using scanner)

               //scan the line

               //increment our counter variable

              

       //return line count

      

       int count = 0;

  

   try {

       Scanner Linecounter = new Scanner(fIn);

      

           while ( Linecounter.hasNextLine()) {

           Linecounter.nextLine();

           count++;

      

           }

       }catch (FileNotFoundException e) {

           System.out.println("could not find file");

       }

      

       return count;

      

      

   }

  

   // in assignemnt we are grabbing 11 numbers and calculating each individual weighted grade then calulating a total grade...need decimal format?

   public static double[] calculateTotals(Scanner fileReader, int numLines) {

  

   //loops through each line

       //prints out the weighted scores on that line

       double [] arr = new double[numLines];

       for(int i =0; i<numLines; i++) {

           //this works for printing out the results but we want an array.

           double a1 = fileReader.nextInt();

           double a2 = fileReader.nextInt();

           double a3 = fileReader.nextInt();

double a4 = fileReader.nextInt();

double a5 = fileReader.nextInt();

double a6 = fileReader.nextInt();

double a7 = fileReader.nextInt();

double a8 = fileReader.nextInt();

double m1 = fileReader.nextInt();

double m2 = fileReader.nextInt();

double f3 = fileReader.nextInt();

double [] weight = {a1, a2, a3, a4, a5, a6, a7, a8, m1, m2, f3};

          

          

           arr[i] = hwGrade(weight[i]);

       }

       return arr;

   }

  

   //exact same for finding the minimum grade, also need a maximum grade and a class average

   public static int min(int[] values) {

   int smallestNumber = values[0];

   //search through the array to fine the smallest number

      

   for(int i = 0; i< values.length; i++) {

       int currentNumber = values[i];

       if (smallestNumber > currentNumber) {

           smallestNumber = currentNumber;

       }

   }

  

  

  

   return smallestNumber;

}

  

  

public static double[] hwGrade(double[] hws) {

double [] result = hws[i];

for (int i=0; i < hws.length; i++) {

  

if (i==0) {

result = hws[i]*1.00;

}

if (i>=1 || i<=8) {

result = hws[i]*.40;

}

if (i==9 || i==10) {

result = hws[i]*15/35;

}

else {

result = hws[i]*37/110;

}

}

  

  

return result;

}

  

  

Explanation / Answer

import java.util.*;
import java.io.*;
class Lab7 {
public static void main(String[] args) {
  
Scanner userInput = new Scanner(System.in);
Scanner fileReader = null;
//ask user to enter the file name
//scan the entry and assign the variable
  
//create File of that file name
//create a Scanner to link to that File.
int numLines = 0;
while (fileReader==null) {
try {
System.out.print("Enter a file name: ");
String fName = userInput.next();
  
File inputFile = new File(fName);
fileReader = new Scanner(inputFile);
  
numLines = getNumEntries(inputFile);
  
  
} catch (FileNotFoundException e) {
System.out.println("Can't scan file!");
}
System.out.println("Success, total lines: " +numLines);
  
double[] totalValues = calculateTotals(fileReader, numLines);
printMethod(totalValues);
//int smallestValue = min(totalValues);
//System.out.println("Smallest value found is: " +smallestValue);
}
  
  
}
// print method for printing what is in the array
public static void printMethod(double [] arr) {
  
//prints out contenets of array
  
for (int i=0; i<arr.length;i++) {
  
//int [] num = arr[i];
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static int getNumEntries(File fIn){
//create variable to keep count of number of lines
  
  
  
//try and create a scanner that lines to the file (again)
//next loop through each line in the file (using scanner)
//scan the line
//increment our counter variable
  
//return line count
  
int count = 0;
  
try {
Scanner Linecounter = new Scanner(fIn);
  
while ( Linecounter.hasNextLine()) {
Linecounter.nextLine();
count++;
  
}
}catch (FileNotFoundException e) {
System.out.println("could not find file");
}
  
return count;
  
  
}
  
// in assignemnt we are grabbing 11 numbers and calculating each individual weighted grade then calulating a total grade...need decimal format?
public static double[] calculateTotals(Scanner fileReader, int numLines) {
  
//loops through each line
//prints out the weighted scores on that line
double [] arr = new double[numLines];
for(int i =0; i<numLines; i++) {
//this works for printing out the results but we want an array.
// double a1 = fileReader.nextInt();
// double a2 = fileReader.nextInt();
// double a3 = fileReader.nextInt();
//double a4 = fileReader.nextInt();
//double a5 = fileReader.nextInt();
//double a6 = fileReader.nextInt();
//double a7 = fileReader.nextInt();
//double a8 = fileReader.nextInt();
//double m1 = fileReader.nextInt();
//double m2 = fileReader.nextInt();
//double f3 = fileReader.nextInt();
//double [] weight = {a1, a2, a3, a4, a5, a6, a7, a8, m1, m2, f3};
  
arr[i]=fileReader.nextInt();
//arr[i] = hwGrade(]);
}
return arr;
}
  
//exact same for finding the minimum grade, also need a maximum grade and a class average
public static int min(int[] values) {
int smallestNumber = values[0];
//search through the array to fine the smallest number
  
for(int i = 0; i< values.length; i++) {
int currentNumber = values[i];
if (smallestNumber > currentNumber) {
smallestNumber = currentNumber;
}
}
  
  
  
return smallestNumber;
}
  
  
public static double[] hwGrade(double[] hws) {
double [] result = new double[hws.length];
for (int i=0; i < hws.length; i++) {
  
if (i==0) {
result[i] = hws[i]*1.00;
}
if (i>=1 || i<=8) {
result[i] = hws[i]*.40;
}
if (i==9 || i==10) {

result[i] = hws[i]*15/35;
}
else {
result[i] = hws[i]*37/110;
}
}
  
  
return result;
}}