Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: File I/O

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation File I/O

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    Exception in thread "main" java.util. MissingFormatArgumentException: Format specifier '52s'"
    Did you look up that error message to see what caused it? It's a class that you can look up in the API doc.
    Then look at the line where the exception happened and see why.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    Quote Originally Posted by Norm View Post
    Did you look up that error message to see what caused it? It's a class that you can look up in the API doc.
    Then look at the line where the exception happened and see why.

    I have no idea what API doc means or anything.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    Having a link to the API doc is important for programming. I look up things there many times a day.
    Here's the link: Java Platform SE 7
    It's where ALL the Java SE classes and their methods are defined.

    Find the class you are interested in the list in the lower left, click on the link and the doc will show in the main frame.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: File I/O

    Wow, this is an overly complex bit of code. It's very hard to follow!

    Do you perhaps have a format specifier without a corresponding argument?

    Check your code at the line specified in the error message. i.e. where it reads:
    		// 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))));

    "%52s" means format a string 52 characters, right justified. However the compiler doesn't know which string to use.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    Hello,

    I have managed to fix this code. I changed the formatting of the pw.println so that it works. The main problem I was having was that I had initialized the PrintWriter and the FileOutputStream above the main module and then again in the fileOutput module, so my program had two different PrintWriters. Another issue was that I was calling my modules before adding to the accumulators, so the values would never have anything in them and would not reach the fileOutput because of the different PrintWriters. Thank you for all your help though!

Similar Threads

  1. Created a Random Access File that saves gibberish to a text file
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 21st, 2012, 11:45 AM
  2. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  3. solaris machine /tmp folder, File.exists() cant see the existing file.
    By aragorn1905 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 27th, 2011, 09:41 AM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM

Tags for this Thread