project11a For this lab you will write a Java program that produces a simple formatted report The program will prompt the user to enter a file name author version 20140923 package osu cse1223 import import import import import import import java io BufferedWriter java io File java io FileWriter java util ArrayList java util HashMap java util List java util Scanner public class project11a private static Scanner sc public static void main String args sc new Scanner System in String fileName int totalParticipants 0 List Integer avgScore new ArrayList Integer HashMap Integer String playerInfo new HashMap Integer String System out println Enter file name fileName sc next try File fileOp new File report txt if fileOp exists fileOp createNewFile FileWriter fw new FileWriter fileOp getAbsoluteFile BufferedWriter bw new BufferedWriter fw System out println Final Overall Scores Report n bw write Final Overall Scores Report bw newLine File file new File fileName Scanner fileScanner new Scanner file bw newLine System out println Name t t tMean tMedian tMax tMin bw write Name t t tMean tMedian tMax tMin n bw newLine System out println bw write bw newLine while fileScanner hasNextLine String playerName fileScanner nextLine List Integer playerScores readNextSeries fileScanner totalParticipants bw newLine avgScore add getAverage playerScores playerInfo put getAverage playerScores playerName System out println formatName playerName t getAverage playerScores t getMedian playerScores t getMax playerScores t getMin playerScores n bw write formatName playerName t getAverage playerScores t getMedian playerScores t getMax playerScores t getMin playerScores bw newLine if fileScanner hasNextLine fileScanner nextLine bw newLine System out println nTotal number of participants totalParticipants bw write Total number of participants totalParticipants bw newLine int maxScore getMax avgScore int minScore getMin avgScore bw newLine System out println Highest average score playerInfo get maxScore maxScore n bw write Highest average score playerInfo get maxScore maxScore bw newLine System out println Lowest average score playerInfo get minScore minScore n bw write Lowest average score playerInfo get minScore minScore bw newLine bw close System out println Writing content to report txt file completed n catch Exception e System out println Exception occured e printStackTrace private static List Integer readNextSeries Scanner inScanner List Integer playerScores new ArrayList Integer while inScanner hasNextLine int score inScanner nextInt if score 1 break else playerScores add score return playerScores private static int getMedian List Integer inList int temp int median int intArray new int inList size for int i 0 i inList size i intArray i inList get i for int i 0 i intArray length 1 i for int j i 1 j intArray length j if intArray i intArray j temp intArray i intArray i intArray j intArray j temp int middle intArray length 2 if intArray length 2 0 int medianA intArray middle int medianB intArray middle 1 median medianA medianB 2 else median intArray middle return median private static int getAverage List Integer inList int avg int sum 0 for int i 0 i inList size i sum sum inList get i avg sum inList size return avg private static int getMin List Integer inList int min inList get 0 for int i 0 i inList size i if inList get i min min inList get i return min private static int getMax List Integer inList int max inList get 0 for int i 0 i inList size i if inList get i max max inList get i return max private static String formatName String name if name length 23 return name else for int i name length i 23 i name name return name
View Full Document