0

I am trying to test a class i'm working on. I want to run a print statement that takes a monthly salary for an employee multiply it by 12 to give me the annual salary then adds 10%. I have gotten everything to work except for the last part

Here is my code so far (This is just partial code)

constructors

 public double findSal(){
    return this.monthlySalary * 12;
}

public double giveRaise(){
    return this.monthlySalary * 12 * 0.10;
}

System.out.printf("The yearly salary for " +employee1.getfirstName()+" " + employee1.getlastName()+" " + "With a 10% raise is: $" +employee1.giveRaise()+ "\n");
System.out.printf("The yearly salary for " +employee2.getfirstName()+" " + employee2.getlastName()+" " + "With a 10% raise is: $" +employee2.giveRaise()+ "\n");

This is the error I am getting when I run

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'r' at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691) at java.util.Formatter$FormatSpecifier.(Formatter.java:2720) at java.util.Formatter.parse(Formatter.java:2560) at java.util.Formatter.format(Formatter.java:2501) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at labex4oop.employeeTest.main(employeeTest.java:35) Java Result: 1

5
  • 2
    What's the question? Commented Nov 21, 2015 at 3:43
  • Neither of the methods shown are constructors, they're just normal methods. Commented Nov 21, 2015 at 3:49
  • Question is why is the last argument the employee1.giveRaise() giving me an error and not working Commented Nov 21, 2015 at 3:52
  • Question title is not matching with your question. Commented Nov 21, 2015 at 3:52
  • Why are you using printf() when you don't plan to use format specifiers? Commented Nov 21, 2015 at 3:56

3 Answers 3

1

Your code suffers from a minor oversight:

Public double giveRaise(){
    return this.monthlySalary * 12.0 * 1.10; // was 0.10
}

You also need to convert from double When printing the values and you have to escape the percent sign in your literals (since you use printf where % has placeholder semantics):

System.out.printf("The yearly salary for " +employee2.getfirstName()+" " + employee2.getlastName()+" " + "With a 10%% raise is: $" +String.valueOf(employee2.giveRaise())+ "\n");
Sign up to request clarification or add additional context in comments.

1 Comment

Still gives me an error after changing it to 1.10. I get this error Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'r' at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691) at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2720) at java.util.Formatter.parse(Formatter.java:2560) at java.util.Formatter.format(Formatter.java:2501) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at labex4oop.employeeTest.main(employeeTest.java:37) Java Result: 1
1
System.out.printf("With [...] a 10% raise [...]");
                                  ^ // your problem is here

printf() is for formatted output. Placeholders in the format string are introduced via %. 10% raise in your code is interpreted as a %r formatting specifier. Since you neither have any arguments to format nor is %r a valid printf format specifier, you get the error message telling you that your format string is wrong.

To include a literal % you must use %%. Or stop using printf() at all because you are not using its capabilities:

System.out.println("The yearly salary for " 
   + employee2.getfirstName()
   + " " + employee2.getlastName()
   + " with a 10% raise is: $" 
   + employee2.giveRaise() + "\n"
);

1 Comment

Thank you it is now working, silly mistake on my part.
0

This should give you the correct answer. I have tried it.public double giveRaise(){ return this.monthlySalary * 12 * 1.10; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.