Hello everyone,
I am in my 2nd term of Computer Software Development and I am nearing the end of my first Java class. My teacher has assigned a 2-part Java program called "Save the Barns". The first part of the program is having the user write records for "Save the Barns PAC"; listing the name, address, city, state, zip, party affiliation, gender identity, and contribution and then outputting those records to a file called "contributors.dat". The first part was a piece of cake for me, however my problems lie in the 2nd part of this project. For the last part we are supposed to input the contributors.dat file but only read in the gender, party affiliation, and contribution. With this information we are to create a summary report called "savethebarns.prt" using formatting that makes the .prt look professional. For some reason I cannot wrap my head around this project. Perhaps it's because of the many snow days we've had, I dunno... but could someone help me? One of the problems I am running into is this: "Exception in thread "main" java.util. MissingFormatArgumentException: Format specifier '52s'"
Any help would be greatly appreciated as it is due tomorrow and I am currently stuck babysitting a not-so-sleepy 3 year old. Thanks!
/** * Save the Barns Part Two *This program reads the contributor file and generates a summary .prt file **/ import javax.swing.*; import java.util.*; import java.io.*; import java.text.*; class SaveTheBarnsPartTwo { //input variables static String iRecord; static String iParty; static String iGender; static double iContribution; //calculated variables static int cRecCtr = 0; static int cMaleCtr = 0, cFemaleCtr = 0; static int cDemCtr = 0, cRepCtr = 0, cIndCtr = 0; static int dMaleCtr = 0, dFemaleCtr = 0; static int rMaleCtr = 0, rFemaleCtr = 0; static int iMaleCtr = 0, iFemaleCtr = 0; static double dMaleContr = 0, dFemaleContr = 0; static double rMaleContr = 0, rFemaleContr = 0; static double iMaleContr = 0, iFemaleContr = 0; //other variables static String eofSw = "N"; //switch to determine end of file static String contrRec = "Y"; //switch to determine contributions over $500 //input-output variables static Scanner myScanner; //scanner for input file static Scanner scanner; //scanner for console static File inFile; static File outFile; static FileOutputStream fos; static PrintWriter pw; static DecimalFormat df1 = new DecimalFormat("0000.00"); static DecimalFormat df2 = new DecimalFormat("00000.00"); static DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy"); public static void main(String [] args) throws IOException { init(); while(!eofSw.equals("Y")) { mainline(); } closing(); } public static void init() throws IOException { scanner = new Scanner(System.in); String lineSeparator = System.getProperty("line.separator"); scanner.useDelimiter(lineSeparator); myScanner = new Scanner("contributors.dat"); inFile = new File("contributors.dat"); myScanner = new Scanner(inFile); outFile = new File("savethebarns.prt"); fos = new FileOutputStream(outFile, true); pw = new PrintWriter(fos); summaryDisplay(); //calls the summary method to display input to output file //print /** inFile = new File("contributors.dat"); myScanner = new Scanner(inFile); inFile = new File("contributors.dat"); myScanner = new Scanner(inFile); scanner = new Scanner(System.in); outFile = new File("SaveTheBarns.prt"); myScanner = new Scanner(outFile, true); String lineSeparator = System.getProperty("line.separator"); myScanner.useDelimiter(lineSeparator); **/ System.out.print("Would you like to only process records with contribution "); System.out.print("over $500?"); contrRec = scanner.next(); //priming read input(); } public static void input() throws IOException { try { //read the record iRecord = myScanner.next(); cRecCtr++; //Add to overall record counter //split the record into fields //only cares to put these 3 fields into a summary .prt iParty = iRecord.substring(72, 73); iGender = iRecord.substring(73, 74); iContribution = Double.parseDouble(iRecord.substring(70, 89)); } catch (Exception e ) { eofSw = "Y"; } } public static void mainline() throws IOException { //output the record //if-statements, add to accumulators, etc. System.out.println(iParty + " " + iGender + " " + iContribution); if(contrRec.equals("Y") || contrRec.equals("y")) { if(iContribution > 500) { if(iGender.equals("M") || iGender.equals("m")) { male(); cMaleCtr++; } else { female(); cFemaleCtr++; } if(iParty.equals("D") || iParty.equals("d")) { cDemCtr++; } else { if(iParty.equals("R") || iParty.equals("r")) { cRepCtr++; } else { cIndCtr++; } } } } else { if(iGender.equals("M") || iGender.equals("m")) { male(); cMaleCtr++; } else { female(); cFemaleCtr++; } if(iParty.equals("D") || iParty.equals("d")) { cDemCtr++; } else { if(iParty.equals("R") || iParty.equals("r")) { cRepCtr++; } else { cIndCtr++; } } } //loop read input(); } public static void male() { switch(iParty) { case "D": case "d": //add to the Democrat/Male counter dMaleCtr++; //add to the Democrat/Male contribution accumulator dMaleContr = dMaleContr + iContribution; break; case "R": case "r": rMaleCtr++; rMaleContr = rMaleContr + iContribution; break; case "I": case "i": iMaleCtr++; iMaleContr = iMaleContr + iContribution; break; } } public static void female() { switch(iParty) { case "D": case "d": dFemaleCtr++; dFemaleContr = dFemaleContr + iContribution; break; case "R": case "r": rFemaleCtr++; rFemaleContr = rFemaleContr + iContribution; break; case "I": case "i": iFemaleCtr++; iFemaleContr = iFemaleContr + iContribution; break; } } public static void summaryDisplay() throws IOException { Date date = new Date(); //write record to the file pw.println(String.format("%52s") + String.format("%14s", "Save The Barns") + String.format("%52s") + String.format("%6s", "Date: ") + String.format("%8s", (dateFormat.format(date)))); pw.println(String.format("%56s") + String.format("%19s", "Contribution Report") + String.format("%57s")); pw.println(" "); pw.println(String.format("%5", "Group") + String.format("%25s") + String.format("%12s", "Record Count") + String.format("%26s") + String.format("%18s", "Total Contribution") + String.format("%27s") + String.format("%20s", "Average Contribution")); } public static void closing() { System.out.println("Thank you for using the Save the Barns PAC program!"); System.out.println("Have a good day!"); } }
The program isn't completely finished here. I have a few more modules to add in.