Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /* Name: Avery Cleland
  2. * Class: CS140-002
  3. * Fall 2016
  4. * Assignment: Programming Assignment 6
  5. * Date: 10/22/2016
  6. */
  7. package assignment6;
  8. import java.util.Scanner;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.PrintWriter;
  12.  
  13. public class Assignment6 {
  14.  
  15. public static void main(String[] args) {
  16. Scanner keyboard = new Scanner(System.in);
  17. int num = 0;
  18. int index;
  19. double average;
  20. String line;
  21. String numstring;
  22. String Name = null;
  23. String filename;
  24. String fileoutname;
  25. System.out.print("Enter the input filename: ");
  26. filename = keyboard.next();
  27. System.out.print("Enther the output filename: ");
  28. fileoutname = keyboard.next();
  29. Scanner inputfile = openInputFile(filename);
  30. PrintWriter outputfile = openOutputFile(fileoutname);
  31. outputfile.printf("%-10s%-8s\t %-8s\n","Name", "# of Attempts", "Score");
  32. while(inputfile.hasNext())
  33. {
  34. line = inputfile.nextLine();
  35. index = line.indexOf(",");
  36. Name = line.substring(0,index);
  37. line = line.substring (index + 1);
  38. index = line.indexOf(",");
  39. numstring = line. substring(0, index);
  40. num = Integer.parseInt(numstring);
  41. line = line.substring(index+1);
  42. average = calculateAverage(filename,num,index,line,inputfile);
  43. outputfile.printf("%-22s%-7s%-8s\n",Name,num,average);
  44.  
  45. }
  46.  
  47. inputfile.close();
  48. outputfile.close();
  49.  
  50.  
  51.  
  52. }
  53. public static Scanner openInputFile(String filename)
  54. {
  55. Scanner filein = null;
  56. try
  57. {
  58. filein = new Scanner(new File(filename));
  59. }
  60. catch (FileNotFoundException ex)
  61. {
  62. System.out.println("Error: Could not open file");
  63. System.exit(1);
  64. }
  65. return filein;
  66. }
  67. public static PrintWriter openOutputFile(String fileoutname)
  68. {
  69. PrintWriter fileout = null;
  70. try
  71. {
  72. fileout = new PrintWriter ("report.txt");
  73. }
  74. catch(FileNotFoundException exc)
  75. {
  76. System.out.println("Error: Could not write file");
  77. System.exit(1);
  78. }
  79. return fileout;
  80. }
  81.  
  82.  
  83. public static double calculateAverage(String filename, String numstring, int num, int index, String line, Scanner inputfile)
  84. {
  85. double avg = 0;
  86. int total = 0;
  87. int i;
  88. for (i = 0; i == num; i++)
  89. {
  90. line = inputfile.nextLine();
  91. index = line.indexOf(",");
  92. numstring = line.substring(0,index);
  93. line = line.substring(index+1);
  94. total =
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. }
  103. avg = total/num;
  104. return avg;
  105. }
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement