Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: Avery Cleland
- * Class: CS140-002
- * Fall 2016
- * Assignment: Programming Assignment 6
- * Date: 10/22/2016
- */
- package assignment6;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class Assignment6 {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- int num = 0;
- int index;
- double average;
- String line;
- String numstring;
- String Name = null;
- String filename;
- String fileoutname;
- System.out.print("Enter the input filename: ");
- filename = keyboard.next();
- System.out.print("Enther the output filename: ");
- fileoutname = keyboard.next();
- Scanner inputfile = openInputFile(filename);
- PrintWriter outputfile = openOutputFile(fileoutname);
- outputfile.printf("%-10s%-8s\t %-8s\n","Name", "# of Attempts", "Score");
- while(inputfile.hasNext())
- {
- line = inputfile.nextLine();
- index = line.indexOf(",");
- Name = line.substring(0,index);
- line = line.substring (index + 1);
- index = line.indexOf(",");
- numstring = line. substring(0, index);
- num = Integer.parseInt(numstring);
- line = line.substring(index+1);
- average = calculateAverage(filename,num,index,line,inputfile);
- outputfile.printf("%-22s%-7s%-8s\n",Name,num,average);
- }
- inputfile.close();
- outputfile.close();
- }
- public static Scanner openInputFile(String filename)
- {
- Scanner filein = null;
- try
- {
- filein = new Scanner(new File(filename));
- }
- catch (FileNotFoundException ex)
- {
- System.out.println("Error: Could not open file");
- System.exit(1);
- }
- return filein;
- }
- public static PrintWriter openOutputFile(String fileoutname)
- {
- PrintWriter fileout = null;
- try
- {
- fileout = new PrintWriter ("report.txt");
- }
- catch(FileNotFoundException exc)
- {
- System.out.println("Error: Could not write file");
- System.exit(1);
- }
- return fileout;
- }
- public static double calculateAverage(String filename, String numstring, int num, int index, String line, Scanner inputfile)
- {
- double avg = 0;
- int total = 0;
- int i;
- for (i = 0; i == num; i++)
- {
- line = inputfile.nextLine();
- index = line.indexOf(",");
- numstring = line.substring(0,index);
- line = line.substring(index+1);
- total =
- }
- avg = total/num;
- return avg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement