aspose file tools*
The moose likes Beginning Java and the fly likes System.out.printf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.printf " Watch "System.out.printf " New topic
Author

System.out.printf

sherri statham
Greenhorn

Joined: Sep 14, 2006
Posts: 6
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


sherri l statham
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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() );


[Jess in Action][AskingGoodQuestions]
sherri statham
Greenhorn

Joined: Sep 14, 2006
Posts: 6
// 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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Presumably the Employee class needs work now: getFirstName() and getLastName() must be returning null, and getMonthlySalary() must be returning 0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.out.printf