// This program converts an input file of written assignment scores from a // boolean (0/1) form to a numerical form. A 0 means someone did not do the // written assignment, a 1 means they did. Students get 3 points for each // written assignment they complete with a maximum of 20 points. The input // file consists of input lines each of which have one student's written // assignment in the 0/1 form. The output file lists the corresponding points. // The output is also echoed to System.out. import java.io.*; import java.util.*; public class Section { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("section.txt")); processFile(input); } // processes the given input file, sending a report to the given output public static void processFile(Scanner input) throws FileNotFoundException { PrintStream outFile = new PrintStream(new File("output.txt")); while (input.hasNextLine()) { //System.out.println(input.nextLine()); String line = input.nextLine(); int[] scores = new int[line.length()]; int totalScore = 0; for (int i = 0; i < scores.length; i++) { if (line.charAt(i) == '1') { if (totalScore + 3 > 20) { scores[i] = 20 - totalScore; totalScore += 20 - totalScore; } else { scores[i] = 3; totalScore += 3; } } else { scores[i] = 0; totalScore += 0; } } outFile.println(line + " -> " + Arrays.toString(scores)); } } }