3 Replies - 8684 Views - Last Post: 06 June 2010 - 02:03 PM Rate Topic: -----

#1 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Format 's' specifier problem using NetBeans

Posted 06 June 2010 - 01:34 PM

I am working on a program to show Polymorphism in NetBeans for the first time and I got an error "Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 's'
at java.util.Formatter.format(Formatter.java:2432)
at java.util.Formatter.format(Formatter.java:2367)
at java.lang.String.format(String.java:2769)
at FlightPay.toString(FlightPay.java:36)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at MilitaryPayScale.main(MilitaryPayScale.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)" when I tried to run the program in NetBeans and I can't figure out what is wrong. Code is below. Please let me know if you can see what is wrong so I can fix it. Of course I have to turn it in tonight. Thanks so much in advance!
//Airman.java
//Airman abstract superclass
 
public abstract class Airman {
    private String firstName;
    private String lastName;
    private String socialSecurityNumber;

    //three-arguement constructor
    public Airman( String first, String last, String ssn )
    {
        firstName = first;
        lastName = last;
        socialSecurityNumber = ssn;
    }//end three-argument Airman constructor

    //set first name
    public void setFirstName( String first )
    {
        firstName = first; //should validate
    }//end method setFirstName

    //return first name
    public String getFirstName()
    {
        return firstName;
    }
    //set last name
    public void setLastName( String last )
    {
        lastName = last; //should validate
    }//end method setLastName

    //return last name
    public String getLastName()
    {
        return lastName;
    }//end method getLastName

    //set social security number
    public void setSocialSecurityNumber( String ssn )
    {
        socialSecurityNumber = ssn; //should validate
    }//end method setSocialSecurityNumber

    //return social security number
    public String getSocialSecurityNumber()
    {
        return socialSecurityNumber;
    }//end method getSocialSecurityNumber

    //return String representation of Airman object
    @Override
    public String toString()
    {
        return String.format( "%s %s\nsocial security number: %s",
        getFirstName(), getLastName(), getSocialSecurityNumber());
    }//end method toString

    //abstract method overridden by concrete subclasses
    public abstract double pay();//no implementation here
}//end abstract class Airman

//BaseMilitaryPay.java
//BaseMilitaryPay concrete class extends abstract class Airman.
public class BaseMilitaryPay extends Airman
{
private double weeklyPay;

//four-argument constructor
public BaseMilitaryPay( String first, String last, String ssn, double pay )
{
    super( first, last, ssn ); //pass to Airman constructor
    setWeeklyPay( pay );//validate and store pay
}//end four-argument BaseMilitaryPay constructor

//set pay
public void setWeeklyPay( double pay )
{
    weeklyPay = pay < 0.0 ? 0.0 : pay;
}//end method setWeeklyPay

//return pay
public double getWeeklyPay()
{
    return weeklyPay;
}//end method getWeeklyPay

//calculate pay; override abstract method pay in Airman
@Override
public double pay()
{
    return getWeeklyPay();
}//end method pay

//return String representation of BaseMilitaryPay object
@Override
public String toString()
{
    return String.format( "Base Military Pay: %s\n%s: $%, .2f",
            super.toString(), "Weekly Pay", getWeeklyPay() );
}//end method toString
}//end class BaseMilitaryPay

//CombatFlightPay.java

public class CombatFlightPay extends CombatPay
{
private double flightPay; //flight pay
private double combatPay;//combat pay

//six-argument constructor
public CombatFlightPay( String first, String last, String ssn, double pay,
double flight, double combat )
{
    super( first, last, ssn, pay, combat );
    setFlightPay( flight );
}//end four-argument constructor

//set flight pay percentage
public void setFlightPay( double flight )
{
    flightPay = ( flight > 0.0 && flight < 1.0 ) ? flight : 0.0;
}//end method setFlightPay

//return flight pay rate
public double getFlightPay()
{
    return flightPay;
}//end method getFlightPay

//calculate pay; override abstract method pay in Airman
@Override
public double pay()
{
    return (getFlightPay() * super.pay()) + (getCombatPay() * super.pay()) + super.pay();
}//end mehtod pay

//return String representation of Combat FlightPay object
@Override
public String toString()
{
    return String.format("%s: %s\n%s: $%, .2f: %s: %.2f:",
    "Combat Flight Pay", super.toString());
}//end method toString
}//end class FlightPay

//CombatPay.java

public class CombatPay extends BaseMilitaryPay
{
private double combatPay; //combat pay
//four-argument constructor
public CombatPay( String first, String last, String ssn, double pay, double combat )
{
    super( first, last, ssn, pay );
    setCombatPay( combat );
}//end four-argument constructor

//set combat pay percentage
public void setCombatPay( double combat )
{
    combatPay = ( combat > 0.0 && combat < 1.0 ) ? combat : 0.0;
}//end method setCombatPay

//return combat pay rate
public double getCombatPay()
{
    return combatPay;
}//end method getCombatPay

//calculate pay; override abstract method pay in Airman
@Override
public double pay()
{
    return getCombatPay() * super.pay()+ super.pay();
}//end mehtod pay

//return String representation of CombatPay object
@Override
public String toString()
{
    return String.format("%s: %s\n%s: $%, .2f: %s: %.2f:",
    "Combat Pay", super.toString());
}//end method toString
}//end class CombatPay

//FlightPay.java

public class FlightPay extends BaseMilitaryPay
{
private double flightPay; //flight pay
//four-argument constructor
public FlightPay( String first, String last, String ssn, double pay, double flight )
{
    super( first, last, ssn, pay );
    setFlightPay( flight );
}//end four-argument constructor

//set flight pay percentage
public void setFlightPay( double flight )
{
    flightPay = ( flight > 0.0 && flight < 1.0 ) ? flight : 0.0;
}//end method setFlightPay

//return flight pay rate
public double getFlightPay()
{
    return flightPay;
}//end method getFlightPay

//calculate pay; override abstract method pay in Airman
@Override
public double pay()
{
    return getFlightPay() * super.pay()+ super.pay();
}//end mehtod pay

//return String representation of FlightPay object
@Override
public String toString()
{
    return String.format("%s: %s\n%s: $%, .2f: %s: %.2f:",
    "Flight Pay", super.toString());
}//end method toString
}//end class FlightPay

//MilitaryPayScale.java

public class MilitaryPayScale
{
	public static void main(String[] agrs)
	{
	BaseMilitaryPay baseMilitaryPay =
		new BaseMilitaryPay("Jeff", "Johnson", "999-99-9999", 1000.00);
	FlightPay flightPay =
		new FlightPay("April", "Smith", "888-88-8888", 1000.00, .10);
	CombatPay combatPay =
		new CombatPay("Darren", "Jones", "777-77-7777", 1000.00, .20);
	CombatFlightPay combatFlightPay =
		new CombatFlightPay("Joe", "Warrant", "666-66-6666", 1000.00, .10, .20);

	//create four-element Airman Array
	Airman[] airman = new Airman[4];

	//Initialize array with Airmen
	airman[0] = baseMilitaryPay;
	airman[1] = flightPay;
	airman[2] = combatPay;
	airman[3] = combatFlightPay;

	for (Airman currentAirman : airman)
	{
		System.out.println(currentAirman); //invokes toString

		System.out.printf(
			"Pay $%, .2f\n\n", currentAirman.pay() );
	}//end for

	}//end main
}//end class MilitaryPayScale




Is This A Good Question/Topic? 0
  • +

Replies To: Format 's' specifier problem using NetBeans

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Format 's' specifier problem using NetBeans

Posted 06 June 2010 - 01:45 PM

In the future, please point out the specific lines of code, not just line numbers. It makes it a lot easier for us to help you debug your code. :)

Your problem is that here return String.format("%s: %s\n%s: $%, .2f: %s: %.2f:", "Flight Pay", super.toString()); , you have multiple formatters (%s and %f), but you only include one String. So you have missing params here, which is causing your errors. If you want to use some attributes from the super class, better to invoke the accessor methods to use them.

This post has been edited by macosxnerd101: 06 June 2010 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: Format 's' specifier problem using NetBeans

Posted 06 June 2010 - 01:54 PM

Sorry about that. I just pasted the error as it appeared in NetBeans and I wasn't exactly sure where the problem was. I'm still really new and I modified the examples in the book to fit my program so I'm not exactly sure what you mean by invoking the accessor methods to use them. Will you please elaborate? Thanks. :surrender:

View Postmacosxnerd101, on 06 June 2010 - 01:45 PM, said:

In the future, please point out the specific lines of code, not just line numbers. It makes it a lot easier for us to help you debug your code. :)

Your problem is that here return String.format("%s: %s\n%s: $%, .2f: %s: %.2f:", "Flight Pay", super.toString()); , you have multiple formatters (%s and %f), but you only include one String. So you have missing params here, which is causing your errors. If you want to use some attributes from the super class, better to invoke the accessor methods to use them.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Format 's' specifier problem using NetBeans

Posted 06 June 2010 - 02:03 PM

If you've gotten this far, then I'm guessing you have at least some familiarity with inheritance. Basically, when you extend an existing class, the subclass inherits all non-private methods, attributes and globals from the super-class, and these elements can be invoked or referenced from the subclass the same as from the super-class. So if you have an accessor/getter method from the super-class, you can invoke it in the subclass.

Here is an example:
class A{
   int getOne(){return 1;}
}

class B extends A{
    int getThree(){return 2 + getOne();}

    //getOne() is defined in A and inherited, so we
    //do not have to redefine it to use it
    //extending A is sufficient
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1