• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System.out.printf

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my program, I've got most of it but my problem is in hte Highlighted area, which is the System.out.printF command. I thought this was right, what I get for thinking, lol

The output is incorrect, because i get the follwing error:
Task05-02, Ch03, Programmed by Michael Statham

Employee list--Before Raise in salary

Name Monthly Salary Annual salary
nullnull 0.00 0.00null Exception in thread "ma
in" java.util.MissingFormatArgumentException: Format specifier 's'
at java.util.Formatter.format(Formatter.java:2429)
at java.io.PrintStream.format(PrintStream.java:899)
at java.io.PrintStream.printf(PrintStream.java:800)
at EmployeeTest.main(EmployeeTest.java:34)

G:\>

public class EmployeeTest
{
// main method begins program execution
public static void main( String args[] )
{
// create Employees object
Employee employee1 = new Employee( "Al Allen\t 2500.0" );
Employee employee2 = new Employee( "Zoe Zigler\t 5000.0" );

// Display The Task ID, and the Programmer
System.out.println( "Task05-02, Ch03, Programmed by Michael Statham\n" );

System.out.println( "Employee list--Before Raise in salary\n" );

System.out.println( "Name\t\t Monthly Salary\t\t Annual salary" );

// get monthly and annual salary for employee1 data


printf Code:
Original - printf Code
System.out.printf( "%",
employee1.getFirstName() );
System.out.printf( "%\t\t",
employee1.getLastName() );
System.out.printf( "%.2f\t\t\t",
employee1.getMonthlySalary() );
System.out.printf( "%.2f",
employee1.getMonthlySalary()*12 );

System.out.printf( "%",
employee1.getFirstName() );
System.out.printf( "%\t\t",
employee1.getLastName() );
System.out.printf( "%.2f\t\t\t",
employee1.getMonthlySalary() );
System.out.printf( "%.2f",
employee1.getMonthlySalary()*12 );
// get monthly and annual salary for employee2 data

print Code:
Original - print Code
System.out.printf( "%s %s\n",
employee2.getFirstName() );
System.out.printf( "%s %s\n",
employee2.getLastName() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary

System.out.printf( "%s %s\n",
employee2.getFirstName() );
System.out.printf( "%s %s\n",
employee2.getLastName() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary()*12 );

employee1.setMonthlySalary(employee1.getMonthlySalary()*1.1);
employee2.setMonthlySalary(employee2.getMonthlySalary()*1.1);

// Display after Raise headline
System.out.println ( "Employee List--After Raise in Salary of 10.0%\n");

// Display heading line for Name, Monthly Salary, and Annual Salary
System.out.println ( "\tName, Monthly Salary, Annual Salary");

// get monthly and annaul salary after 10.0% raise for employee1 data

print Code:
Original - print Code
System.out.printf( "%s %s\n",
employee1.getFirstName() );
System.out.printf( "%s %s\n",
employee1.getLastName() );
System.out.printf( "%s %s\n",
employee1.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee1.getMonthlySalary()*12 );

System.out.printf( "%s %s\n",
employee1.getFirstName() );
System.out.printf( "%s %s\n",
employee1.getLastName() );
System.out.printf( "%s %s\n",
employee1.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee1.getMonthlySalary()*12 );
// get monthly and annual salary after 10.0% raise for employee2 data

print Code:
Original - print Code
System.out.printf( "%s %s\n",
employee2.getFirstName() );
System.out.printf( "%s %s\n",
employee2.getLastName() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary()*12 );

System.out.printf( "%s %s\n",
employee2.getFirstName() );
System.out.printf( "%s %s\n",
employee2.getLastName() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary() );
System.out.printf( "%s %s\n",
employee2.getMonthlySalary()*12 );
// Display a blank line follwed by the end of program line
System.out.println ( "End of program\n");

} // end method main
} // end class EmployeeTest
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

It seems like you're showing multiple copies of different parts of your program, and I can't really follow what you're trying to say with those. But I see both

System.out.printf( "%s %s\n",
employee2.getFirstName() );


and

System.out.printf( "%",
employee1.getFirstName() );


both of which are wrong. The first one uses two format specifiers ("%s %s"), which is only correct if you're intending to pass two arguments to be printed, but you're only passing one.

In the second one, there is only a "%" sign, which by itself is missing a conversion specifier and so isn't a valid format.

The correct version would use just one valid format specifier:

System.out.printf( "%s",
employee1.getFirstName() );
 
sherri statham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// get monthly and annual salary for employee1 data
System.out.printf("%s",
employee1.getFirstName());
System.out.printf("%s\t\t",
employee1.getLastName() );
System.out.printf("%.2f\t\t\t",
employee1.getMonthlySalary() );
System.out.printf("%.2f\n",
employee1.getMonthlySalary()*12 );

// get monthly and annual salary for employee2 data
System.out.printf("%s",
employee2.getFirstName() );
System.out.printf("%s\t\t",
employee2.getLastName() );
System.out.printf("%.2f\t\t\t",
employee2.getMonthlySalary() );
System.out.printf("%.2f\n",
employee2.getMonthlySalary()*12 );

// give both Employee's a 10% raise
employee1.setMonthlySalary(employee1.getMonthlySalary()*1.1);
employee2.setMonthlySalary(employee2.getMonthlySalary()*1.1);

// Display after Raise headline
System.out.println ( "\nEmployee List--After Raise in Salary of 10.0%\n");

// Display heading line for Name, Monthly Salary, and Annual Salary
System.out.println ( "Name\t\t Monthly Salary\t\t Annual Salary");

// get monthly and annaul salary after 10.0% raise for employee1 data
System.out.printf("%s",
employee1.getFirstName() );
System.out.printf("%s\t\t",
employee1.getLastName() );
System.out.printf("%.2f\t\t\t",
employee1.getMonthlySalary() );
System.out.printf("%.2f\t\t\t\n",
employee1.getMonthlySalary()*12 );

// get monthly and annual salary after 10.0% raise for employee2 data
System.out.printf("%s",
employee2.getFirstName() );
System.out.printf("%s\t\t",
employee2.getLastName() );
System.out.printf("%.2f\t\t\t",
employee2.getMonthlySalary() );
System.out.printf("%.2f\t\t\t\n",
employee2.getMonthlySalary()*12 );
// Display a blank line follwed by the end of program line
System.out.println ( "\nEnd of program");

but the print out is now

Task 05-02, Ch03, Programmed by Michael Statham

Employee list--Before Raise in salary

Name Monthly Salary Annual salary
nullnull 0.00 0.00
nullnull 0.00 0.00

Employee List--After Raise in Salary of 10.0%

Name Monthly Salary Annual Salary
nullnull 0.00 0.00
nullnull 0.00 0.00

End of program

G:\>
null need to be names, and the 0.00 needs to be $2500.0
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably the Employee class needs work now: getFirstName() and getLastName() must be returning null, and getMonthlySalary() must be returning 0.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic